1. 目的
你是否在做一個表格排序,但只能知道某幾個行之間的相對順序,而可能排著排著發現后面還有順序更靠前的項,而不得不將排好的序號重新+1+1……
所以你需要一個宏,它可以知道你輸入了一個已經存在的序號,并以那個序號為準,自動修改其他序號
舉兩個例子:
原始 | 修改 | 修改后 |
---|---|---|
3 | 3 | |
3 | 4 | |
7 | 7 | |
2 | 2 | |
1 | 1 | |
原始 | 修改 | 修改后 |
---|---|---|
3 | 4 | |
4 | 5 | |
7 | 2 | 2 |
2 | 3 | |
1 | 1 | |
2. 宏代碼
Sub Worksheet_Change(ByVal Target As Range)If Not Intersect(Target, Target.Worksheet.Range("A:A")) Is Nothing ThenDim ws As WorksheetSet ws = Target.WorksheetDim userInputRow As IntegeruserInputRow = Target.RowDim userInputValue As IntegeruserInputValue = Target.ValueDim i As IntegerDim lastRow As IntegerlastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).RowFor i = 1 To lastRowIf i <> userInputRow ThenIf ws.Cells(i, 1).Value = userInputValue Thenws.Cells(i, 1).Value = userInputValue + 1End IfEnd IfNext iEnd If
End Sub
宏怎么用就不多說了,這里是把排序列定為A列,可以按需改