2015-08-17 37 views
1

任何人都可以解釋我如何評估我的行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

您可以通過使用過濾器並刪除一次傳遞中的空白行來擺脫一些循環。它是否像你以後那樣調整? – sous2817

+0

在wsCountry表是列A到P整行?另外,您想要刪除表格上的空白行嗎? –

回答

3

使用2個自動篩選:

  1. 除去行,其中柱C包含空值
  2. 除去其中塔E不包含在說明書片細胞C9

的值的行。

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 
+0

有沒有辦法從第8行開始自動篩選? – Chris2015

+0

我更新了答案 –

1

你的代碼似乎很好。但是,您正在尋求最佳性能。這可以通過在數組上循環而不是範圍來完成,當範圍很大時可能會變慢。最好將範圍轉儲到一個陣列中,如下所示:

Dim Arr() As Variant 
Arr = Sheet1.Range("A1:B10") 

而是在數組上循環。由於這會創建一個2d數組,因此您可以選擇僅在其中一個維(行)上循環。我希望我能正確回答你的問題。