任何人都可以解释我如何评估我的行e作为一个数组而不是单个(行)级别?这是提高性能的最佳方法吗?
Dim x As Integer, i As Integer, y As Long
Set wsCountry = Sheets("Country Data")
Set wsInstructions = Sheets("Instructions")
LastRow = wsCountry.Range("E" & Rows.Count).End(xlUp).Row
For x = 9 To 9
For i = 9 To LastRow
If wsCountry.Range("E" & i) <> wsInstructions.Range("C" & x) Then
wsCountry.Range("A" & i & ":P" & i).ClearContents
End If
Next i
'Delete Blanks
For y = Cells(Rows.Count, 3).End(xlUp).Row To 1 Step -1
If Cells(y, 3) = "" Then
Rows(y).Delete
End If
Next y
'Save workbook AS to FILE
Next x
答案 0 :(得分:3)
使用2个自动过滤器:
Option Explicit
Sub parseRows()
Dim wsCountry As Worksheet, wsInstructions As Worksheet
Dim lastRow As Long, instructionsVal As String, rowRng As Range
Set wsCountry = Worksheets("Country Data")
Set wsInstructions = Worksheets("Instructions")
instructionsVal = wsInstructions.Range("C9")
Application.ScreenUpdating = False
lastRow = wsCountry.Cells(wsCountry.Rows.Count, 5).End(xlUp).Row
Set rowRng = wsCountry.Range("C8:C" & lastRow)
With wsCountry.UsedRange 'Filter col 3 (C) - make blanks visible
.AutoFilter Field:=3, Criteria1:="="
rowRng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilter
End With
lastRow = wsCountry.Cells(wsCountry.Rows.Count, 5).End(xlUp).Row
Set rowRng = wsCountry.Range("E8:E" & lastRow)
With wsCountry.UsedRange 'Filter col 5 (E) - values <> instructionsVal
.AutoFilter Field:=5, Criteria1:="<>" & instructionsVal
rowRng.SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilter
End With
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:1)
您的代码似乎很好。但是,您要求获得最佳性能。这可以通过循环数组而不是范围来完成,当范围很大时,这可能会变慢。最好将范围转储到数组中,如下所示:
Dim Arr() As Variant
Arr = Sheet1.Range("A1:B10")
然后在数组上循环。由于这会创建一个二维数组,因此您可以选择仅在其中一个维(行)上循环。我希望我能正确回答你的问题。