2013-07-03 72 views
0

下面的代碼允許我刪除行,如果一個單元格包含某些值。現在由於某種原因,我花了很多時間(30分鐘和計數)。更快刪除行

' to delete data not meeting criteria 
       Worksheets("Dashboard").Activate 
       n1 = Range("n1") 
       n2 = Range("n2") 
       Worksheets("Temp Calc").Activate 
       lastrow = Cells(Rows.Count, 1).End(xlUp).Row 
       For z = lastrow To 2 Step -1 
       If Cells(z, 6).Value = "CNF" Or Cells(z, 4).Value <= n1 Or Cells(z, 3).Value >= n2 Then 
       Rows(z).Delete 
       End If 
       Next z 

谷歌搜索,並與論壇成員SAM一些談話給我提供了兩個選項

  1. 使用過濾器。(我也想用這個)。
  2. 使用數組來存儲整個工作表,然後複製只符合我的條件的數據。他非常友好,可以幫我拿出下面的代碼。但是我不熟悉處理數組中的數據。

    lastrow = Cells(Rows.Count, 1).End(xlUp).Row 
    lastCol = Cells(1, Column.Count).End(xlRight).Row 
    arr1 = Range("A1:Z" & lastrow) 
    ReDim arr2(1 To UBound(arr1, 1), 1 To UBound(arr1, 2)) 
    j = j + 1 
    For i = 1 To UBound(arr1, 1) 
    If arr1(i, 6) <> "CNF" And arr1(i, 4) > n1 And arr1(i, 3) < n2 Then 
    For k = 1 To lastCol 
        arr2(j, k) = arr1(i, k) 
    Next k 
    j = j + 1 
    End If 
    Next i 
    
    
    Range(the original bounds) = arr2 
    

我的問題是是否有比上面提到的那些以外的數組刪除行的一個更快的方法?或者是數組或篩選我所擁有的最佳選擇。我樂於接受建議。

更新我的新代碼看起來像這樣。它不會過濾日期範圍,如果它們是硬編碼的,任何人都可以告訴我我做錯了什麼?

Option Explicit 

Sub awesome() 
Dim Master As Workbook 
Dim fd As FileDialog 
Dim filechosen As Integer 
Dim i As Integer 
Dim lastrow, x As Long 
Dim z As Long 
Application.ScreenUpdating = False 
Dim sngStartTime As Single 
Dim sngTotalTime As Single 
Dim ws As Worksheet 
Dim FltrRng As Range 
Dim lRow As Long 
Dim N1 As Date, N2 As Date 

sngStartTime = Timer 
Sheets("Dashboard").Select 
N1 = Range("n1").Value 
N2 = Range("n2").Value 
Sheets("Temp Calc").Select 

'Clear existing sheet data except headers 
'Sheets("Temp Calc").Select 
'Rows(1).Offset(1, 0).Resize(Rows.Count - 1).ClearContents 

'The folder containing the files to be recap'd 
Set fd = Application.FileDialog(msoFileDialogFilePicker) 
fd.InitialFileName = "G:\Work\" '<----- Change this to where the files are stored. 
fd.InitialView = msoFileDialogViewList 
'allow multiple file selection 
fd.AllowMultiSelect = True 
fd.Filters.Add "Excel Files", "*.xls*" 
filechosen = fd.Show 
'Create a workbook for the recap report 
Set Master = ThisWorkbook 
If filechosen = -1 Then 

'open each of the files chosen 
For i = 1 To fd.SelectedItems.Count 
Workbooks.Open fd.SelectedItems(i) 
With ActiveWorkbook.Worksheets(1) 
Range("O2", Range("O" & Cells(Rows.Count, "O").End(xlUp).Row)).Copy Master.Worksheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1, 0) 
Range("p2", Range("P" & Cells(Rows.Count, "P").End(xlUp).Row)).Copy Master.Worksheets(2).Range("B" & Rows.Count).End(xlUp).Offset(1, 0) 
Range("Q2", Range("Q" & Cells(Rows.Count, "Q").End(xlUp).Row)).Copy Master.Worksheets(2).Range("C" & Rows.Count).End(xlUp).Offset(1, 0) 
Range("R2", Range("R" & Cells(Rows.Count, "R").End(xlUp).Row)).Copy Master.Worksheets(2).Range("D" & Rows.Count).End(xlUp).Offset(1, 0) 
Range("A2", Range("A" & Cells(Rows.Count, "A").End(xlUp).Row)).Copy Master.Worksheets(2).Range("E" & Rows.Count).End(xlUp).Offset(1, 0) 
Range("AC2", Range("AC" & Cells(Rows.Count, "AC").End(xlUp).Row)).Copy Master.Worksheets(2).Range("F" & Rows.Count).End(xlUp).Offset(1, 0) 
End With 
' Sheets(1).Range("D4", Sheets(1).Range("D" & Sheets(1).Cells(Rows.Count, "D").End(xlUp).Row)).Copy Sheets(2).Range("B" & Sheets(2).Rows.Count).End(xlUp).Offset(1, 0) 
ActiveWorkbook.Close (False) 
Next i 
End If 

Set ws = ThisWorkbook.Worksheets("Temp Calc") 

'~~> Start Date and End Date 
N1 = #5/1/2012#: N2 = #7/1/2012# 

With ws 

'~~> Remove any filters 
.AutoFilterMode = False 

'~~> Get the last row 
lRow = .Range("A" & .Rows.Count).End(xlUp).Row 

'~~> Identify your data range 
Set FltrRng = .Range("A1:F" & lRow) 

'~~> Filter the data as per your criteria 
With FltrRng 
'~~> First filter on blanks 
.AutoFilter Field:=6, Criteria1:="=" 

Application.ScreenUpdating = False 
Application.Calculation = xlCalculationManual 
'~~> Delete the filtered blank rows 
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete 

ws.ShowAllData 

'~~> Next filter on Start Date 
.AutoFilter Field:=3, Criteria1:="<" & N1, Operator:=xlAnd 
'~~> Finally filter on End Date 
.AutoFilter Field:=4, Criteria1:=">" & N2, Operator:=xlAnd 

'~~> Filter on col 6 for CNF 
'.AutoFilter Field:=6, Criteria1:="CNF" 

'~~> Delete the filtered rows 
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete 
End With 

Application.ScreenUpdating = True 
Application.Calculation = xlCalculationAutomatic 

'~~> Remove any filters 
.AutoFilterMode = False 
End With 

sngTotalTime = Timer - sngStartTime 
MsgBox "Time taken: " & (sngTotalTime \ 60) & " minutes, " & (sngTotalTime Mod 60) & " seconds" 

Application.Goto (ActiveWorkbook.Sheets("Dashboard").Range("A4")) 
Sheets("Dashboard").Select 
Application.ScreenUpdating = True 
End Sub 
+2

我會使用過濾是非常快... –

+0

我讀來實現關於它,但沒有其他辦法? 哦,這裏是一個踢球者,相同的代碼昨天在2分鐘內工作,今天它已經45分鐘,仍然計數 – mathew

+1

爲同一組數據?差異太大,可能是你的代碼或數據有問題... –

回答

0

這對我的作品.....謝謝大家....它採用了先進的過濾器

Dim x, rng As Range 
    x = Array("BENIN-00001", "BENIN-00002", "BENTB-0001", "BENTB-0002", "BENTB-0003", "BENTB-0004", _ 
    "BENTB-0005", "BENTB-0006", "BENTB-0007", "BENTB-0008", "BENTH-00001", "CRPTB-00002", "GDSGL-00001", _ 
    "GDSIN-00001", "GDSIN-00002", "GDSIN-00003", "LSIED-00001", "LSIES-00001", "PRSGS-00001", "PRSGS-00002", _ 
    "PRSGS-00003", "PRSGS-00006", "PRSGS-00007", "PRSGS-00008", "PRSPS-00001", "PRSPS-00002", "PRSTB-0001", _ 
    "PRSTB-0002", "PRSTB-0003", "PRSTB-0004", "PRSTB-0005", "PRSTB-0006", "PRSTB-0007", "SNMIN-00001", "SNMIN-00002", _ 
    "TRGIN-00001", "TRGIN-00002", "TRGTH-00001", "BENEU-00002", "BENEU-00003", "GDSEU-00002", "GDSEU-00003", _ 
    "GDSEU-00004", "PRSGS-00005", "PRSGS-00061", "PRSPS-00004", "PRSPS-00005", "TRGEU-00002", "TRGGB-00001", _ 
    "BENMX-00001", "BENUS-00001", "BENUS-00002", "GDSCA-00001", "GDSGL-00002", "GDSMX-00001", "GDSUS-00001", _ 
    "GDSUS-00002", "LSIPP-00001", "PRSGS-00004", "PRSPS-00003", "TRGMX-00001", "TRGUS-00001") 
    With Sheets("Temp Calc").Cells(1).CurrentRegion 
     On Error Resume Next 
     .Columns(6).SpecialCells(4).EntireRow.Delete 
     On Error GoTo 0 
     Set rng = .Offset(, .Columns.Count + 1).Cells(1) 
     .Cells(1, 5).Copy rng 
     rng.Offset(1).Resize(UBound(x) + 1).Value = Application.Transpose(x) 
     .AdvancedFilter 1, rng.CurrentRegion 
     .Offset(1).EntireRow.Delete 
     On Error Resume Next 
     .Parent.ShowAllData 
     On Error GoTo 0 
     rng.EntireColumn.Clear 
    End With