|
给一个不同的思路。
- Sub fyExcelVBA()
- Dim arr, brr, arr1, i&, j%, nRow%, nCol%, str$
- Dim dic As Object
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set dic = CreateObject("scripting.dictionary")
- With Sheets("sheet1")
- nRow = .Range("a" & Rows.Count).End(3).Row
- nCol = .Cells(1, Columns.Count).End(1).Column
- arr = .Range("a1").Resize(nRow, nCol - 1)
- brr = .Range(.Cells(1, nCol), .Cells(nRow, nCol))
- End With
- For i = 2 To UBound(arr)
- If arr(i, 2) < 20 Or arr(i, 3) < 180 Then
- For j = 1 To UBound(arr, 2)
- arr(i, j) = ""
- Next j
- End If
- Next i
- str = "中间转换"
- Sheets.Add.Name = str
- With Sheets(str).Range("a1").Resize(nRow, nCol - 1)
- .Value = arr
- .Sort key1:=.Cells(1, nCol - 1), order1:=xlDescending
- End With
- arr1 = Sheets(str).Range("a1").CurrentRegion
- For i = 2 To UBound(arr1)
- n = n + 1
- dic(arr1(i, 1)) = n
- Next i
- For i = 2 To UBound(brr)
- If Not dic.exists(arr(i, 1)) Then
- brr(i, 1) = ""
- Else
- brr(i, 1) = dic(arr(i, 1))
- End If
- Next i
- With Sheets("sheet1")
- .Range(.Cells(1, nCol), .Cells(nRow, nCol)) = brr
- End With
- Set dic = Nothing
- Sheets(str).Delete
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- End Sub
复制代码 |
|