将工作表存储在一个数组中并对其进行处理

下面的代码允许我删除不符合我的标准的行,现在问题是大约需要52分钟,因为我的行数超过了1,68,000,并且每周都会增加。 现在我使用filter来减less基于我的cnf和条件的数据,因为date范围不能被过滤掉。 但是这不是一个选项。 我不得不根据我的date比较范围删除行。 看起来数组是我最后的手段,但我不知道如何将我的工作表存储在数组中,并在其上工作。 有人请指导我吗?

[CODE] ' 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 [/CODE] 

这是基本的尝试,让我想起我对arrays知之甚less,所以下面可能是错误的和不完整的

 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 ? 

我吮吸数组。 任何帮助表示赞赏

确定我们的聊天讨论,这里是我的自动过滤方法。

您的要求

首先,我想要删除行cole 6中有“”我也存储两个datevariablesn1和n2现在如果col 4> n1和col 3 <n2然后删除请忽略CNF条件,因为我的一些数据有一些例外这是我想保持的

假设你的数据看起来像这样

在这里输入图像说明

现在让我们说N1 = 5/1/2012N2 = 7/1/2012

如果你直观地看到截图,那么你会注意到只有一行符合条件,那就是第9行(Employee 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 

截图后

如果您注意到所需的logging已被删除。

在这里输入图像说明

尝试这个 :

  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