以下代码会删除与我的条件不符的行。我的行超过1,68,000,大约需要52分钟,而且还会不断增加。
我使用过滤器来减少数据。这不再是一种选择。我必须根据我的日期比较范围删除行。似乎数组是我最后的手段,但我不知道如何将我的工作表存储在数组中并对其进行处理。
如何将工作表存储在数组中并进行处理?
' 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
这是基本的东西。
Dim arr1(), dim arr2() as variant
lastrow = cells(Rows.count,1).End(XlUp).Row
lastcol = cells(1,column.count).End(xlRight).Column
arr1(lastrow,lastcol) <- I dont know if this is correct.
<------How do I copy/paste my data into the array? ----->
<This is what I came up with for deleting what I dont need.>
For x=lastrow to 2 Step -1
If arr1(x,6)<>"" or arr1(x,6)<>"CNF" And arr(x,4)>=n1 And arr(x,3)<=n2 then
For k = lastrow to 2
<I dont know how to delete rows in an array.
rows(x).delete ?
答案 0 :(得分:4)
我们的聊天讨论更进一步,这是我的自动过滤方法。
您的要求
首先我要删除cole 6中有“”的行 我也在变量n1和n2中存储了两个日期 现在如果col 4&gt; n1和col 3&lt; n2然后删除 请忽略CNF条件,因为我的一些数据有一些例外,我想保留
假设您的数据看起来像这样
现在让我们说N1 = 5/1/2012
和N2 = 7/1/2012
如果您直观地看到屏幕截图,那么您会注意到只有一行符合条件,即第9行(员工623 ***)。
<强>代码强>
我已对代码进行了评论,以便您在理解代码时不会遇到任何问题。
Sub Sample()
Dim ws As Worksheet
Dim FltrRng As Range
Dim lRow As Long
Dim N1 As Date, N2 As Date
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:="="
'~~> Next filter on Start Date
.AutoFilter Field:=3, Criteria1:=">" & N1, Operator:=xlAnd
'~~> Finally filter on End Date
.AutoFilter Field:=4, Criteria1:="<" & N2, Operator:=xlAnd
'
'~~> And so on if required
'
'~~> Delete the filtered rows
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
'~~> Remove any filters
.AutoFilterMode = False
End With
End Sub
屏幕截图
如果您发现所需记录已被删除。
答案 1 :(得分:3)
试试这个:
Dim varArrdata as variant
Dim lngloop as long
Dim strRows as string
vararrdata = Range(Cells(1, 1), Cells(Rows.Count, 6).End(xlUp)) ' OR use Range("A1").CurrentRegion
For lngLoop = LBound(vararrdata) To UBound(vararrdata)
If vararrdata(lngLoop, 6) = "CNF" Or vararrdata(lngLoop, 4) <= [n1] Or vararrdata(lngLoop, 3) >= [n2] Then
strRows = strRows & "|" & lngLoop
End If
Next
vararrdata = Split(Mid(strRows, 2), "|")
Range("A" & Join(vararrdata, ",A")).EntireRow.Delete