2

下面的代码删除不符合我的条件的行。当我的行数超过 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 ?
4

2 回答 2

4

好吧,继续我们的聊天讨论,这是我的自动过滤方法。

您的要求

首先,我想删除 cole 6 中具有“”的行,如果 col 4 > n1 和 col 3 < n2,我现在将两个日期存储在变量 n1 和 n2 中,然后删除请忽略 CNF 条件,因为我的一些数据有一些例外情况这个我想保留

假设您的数据如下所示

在此处输入图像描述

现在让我们N1 = 5/1/2012N2 = 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

截图后

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

在此处输入图像描述

于 2013-07-05T05:37:51.733 回答
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
于 2013-07-05T05:17:12.293 回答