进行得太久

时间:2015-08-07 14:31:37

标签: excel vba

我正在使用此代码

For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "100" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow
For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "105" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow
For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "113" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow
For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "120" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow
For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "123" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow
For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "124" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow
For lrow = Lastrow To Firstrow Step -1
Set workrange = Cells(lrow, 5)
Set workrange2 = Cells(lrow, 10)
If workrange.Value = "125" _
 And workrange2.Value = "0" _
        Then workrange.EntireRow.Delete
Next lrow

我得到的过程太长了,因为我有相同的代码但是代码的workrange.value部分有多个其他值。是否有任何方法可以对此代码进行流式处理,以便我可以将所需的任何值用于workrange.value,而不需要使用大量代码?谢谢!

1 个答案:

答案 0 :(得分:2)

要使用循环来解决这个问题,首先请记住,您将希望尽可能地限制循环次数 - 不必要地循环将需要额外的计算时间。一般来说,我们将创建一个特定的数组'包含要匹配的所有数字的完整列表的值。我们将创建一个循环,检查该值是否等于任何Array值,但在我们执行该循环之前,我们将首先检查另一个值是否为0.

我无法看到在确定您关注的值时遵循的模式,因此我展示了如何手动将每个值插入数组中。如果有一些模式,我没有看到,这可以做不同的。或者,如果您已在Excel中使用这些数字,则可以将该Excel行/列设置为命名范围,然后循环执行该操作,而无需创建自己的数组。

如果遇到匹配则编辑以下内容以退出For循环,以防止尝试检查已删除的行的值

Dim CheckArray() As Integer
Dim CheckArraySize As Integer
Dim ArrayCounter As Integer

CheckArraySize = 7
ReDim CheckArray(1, CheckArraySize)

CheckArray(1) = "100" 'Note that these are TEXT values, not numbers,
CheckArray(2) = "105" 'Per the examples in the OP
CheckArray(3) = "113"
CheckArray(4) = "120"
CheckArray(5) = "123"
CheckArray(6) = "124"
CheckArray(7) = "125"


For lrow = Lastrow To Firstrow Step -1
    Set workrange = Cells(lrow, 5)
    Set workrange2 = Cells(lrow, 10)
    If workrange2.Value = "0" Then
        For ArrayCounter = 1 To CheckArraySize
            If workrange.Value = CheckArray(ArrayCounter) Then
                workrange.EntireRow.Delete
                ArrayCounter = CheckArraySize
            End If
        Next ArrayCounter
    End If
Next lrow

@xidgel在评论中建议的替代方法 - 运行得更快

Dim CheckArray() As Integer
Dim CheckArraySize As Integer
Dim ArrayCounter As Integer
Dim CheckString as String

CheckArraySize = 7
ReDim CheckArray(1, CheckArraySize)

CheckArray(1) = "100" 'Note that these are TEXT values, not numbers,
CheckArray(2) = "105" 'Per the examples in the OP
CheckArray(3) = "113"
CheckArray(4) = "120"
CheckArray(5) = "123"
CheckArray(6) = "124"
CheckArray(7) = "125"

For ArrayCounter = 1 to CheckArraySize
    CheckString = CheckString & CheckArray(ArrayCounter) &":"
Next ArrayCounter 'This loops to create a single string which contains all values that you want to check against

For lrow = Lastrow To Firstrow Step -1
    Set workrange = Cells(lrow, 5)
    Set workrange2 = Cells(lrow, 10)
    If workrange2.Value = "0" Then
        If InStr(1, CheckString, workrange.Value) > 0 Then 'checks if workrange is found in the compilation of all array values
            workrange.EntireRow.Delete
        End If
    End If
Next lrow

第二次编辑以反映'搜索条件'已经位于Excel

此方法与上面的方法基本相同,只是不是定义新数组,而是从Excel中调用数组,它已经存储在那里。你说'搜索条件'已经在另一本工作簿中。我假设它们在一个列中。将该列复制到您当前的工作簿,让我们在sheet2中说!列A.选择所有这些条目;转到Formulas功能区 - >名称管理员 - >新名称。将此新名称称为CheckArray。要在VBA中引用此新名称,您只需调用Sheets(2).Range(" CheckArray")。其他所有内容基本上都与上面的相同,只有CheckString的开始赋值更改,就像这样[也删除" Dim CheckArray()语句]:

CheckArraySize = Sheets(2).Range("CheckArray").Rows.Count

For ArrayCounter = 1 to CheckArraySize
    CheckString = CheckString & Sheets(2).Range("CheckArray")(ArrayCounter,1) &":"
Next ArrayCounter

除了如何将字符串值拉入CheckString之外,其余代码将是相同的。