加快VBA循环

时间:2018-12-14 10:08:46

标签: excel vba excel-vba performance loops

每周工作的我都有大约15000个客户的文件,根据他们的名字,我需要将它们分为两类。我当前的代码可以运行,但是它循环遍历每行,几乎需要运行3分钟。提高速度的最佳方法是什么-我假设有比使用冗长的if语句更有效的方法?

Option Compare Text

Private Sub CommandButton1_Click()

Dim i As Long

Application.ScreenUpdating = False

For i = 2 To Rows.Count

    If Cells(i, 33).Value = "Business" Then
        Cells(i, 32).Value = "B"
    ElseIf Cells(i, 33).Value = "Personal" Then
        Cells(i, 32).Value = "P"
    ElseIf Cells(i, 12).Value = "N" Then
        Cells(i, 32).Value = "B"
    ElseIf Cells(i, 12).Value = "Y" Then
        Cells(i, 32).Value = "P"
    ElseIf Cells(i, 20).Value = "PREMIER" Then
        Cells(i, 32).Value = "P"
    ElseIf InStr(1, Cells(i, 4), "LTD") <> 0 Then 'Finds each word in customer name, column D, and enters it as business customer
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "LIMITED") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "MANAGE") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "BUSINESS") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "CONSULT") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "INTERNATIONAL") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "T/A") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "TECH") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "CLUB") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "OIL") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "SERVICE") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf InStr(1, Cells(i, 4), "SOLICITOR") <> 0 Then
        Cells(i, 32).Value = "B"
    ElseIf Cells(i, 4).Value = "UIT" Then
        Cells(i, 32).Value = "B"
    Else
        Cells(i, 32).Value = ""
    End If
Next i
Application.ScreenUpdating = True

End Sub

2 个答案:

答案 0 :(得分:1)

如果您想加快这一过程,我将停止使用VBA,而是写一个公式。

示例:要查找一个单元格是否等于“ Business”或“ N”,您可以使用类似以下的方法:

=IF(OR(A1="Business";A2="N");"B";"P")

要查找某个单元格是否包含“业务”,可以使用以下内容:

=IF(FIND("Business";A1);"B";"P")

使用OR()工作表功能将所有这些组合在一起,您可以得到完整的东西。显然,您需要将公式拖到工作表中的整个列上。

答案 1 :(得分:0)

尝试

Private Sub CommandButton1_Click()

    Dim i As Long, r As Long
    Dim vDB As Variant
    Dim Ws As Worksheet
    Dim rngDB As Range

    Set Ws = ActiveSheet
    Set rngDB = Ws.UsedRange
    vDB = rngDB
    r = UBound(vDB, 1)



    For i = 2 To r

        If vDB(i, 33) = "Business" Then
            vDB(i, 32) = "B"
        ElseIf vDB(i, 33) = "Personal" Then
            vDB(i, 32) = "P"
        ElseIf vDB(i, 12) = "N" Then
            vDB(i, 32) = "B"
        ElseIf vDB(i, 12) = "Y" Then
            vDB(i, 32) = "P"
        ElseIf vDB(i, 20) = "PREMIER" Then
            vDB(i, 32) = "P"
        ElseIf InStr(1, vDB(i, 4), "LTD") <> 0 Then 'Finds each word in customer name, column D, and enters it as business customer
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "LIMITED") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "MANAGE") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "BUSINESS") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "CONSULT") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "INTERNATIONAL") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "T/A") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "TECH") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "CLUB") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "OIL") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "SERVICE") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf InStr(1, vDB(i, 4), "SOLICITOR") <> 0 Then
            vDB(i, 32) = "B"
        ElseIf vDB(i, 4) = "UIT" Then
            vDB(i, 32) = "B"
        Else
            vDB(i, 32) = ""
        End If
    Next i
    rngDB = vDB
End Sub