0

我在 VBA 中编写了一个小脚本。假设我在 Excel 工作表 ( sheet1) 中有数据,并希望在特定条件下应用过滤器。如果列AJ:AJ包含“客户”一词,则应用过滤范围B:BElse Msgbox("Customer NA")。我目前正在使用下面的代码,但它使 Excel 冻结。任何帮助都非常感谢。

Sub test()
    For i = 1 To 1048576
        Range("AJ" & i).Select
        ss = Len(ActiveCell.Value)
        For j = 1 To ss
            dd = StrConv(Mid(ActiveCell.Value, j, 8), vbProperCase)
            If dd = "Customer" Then
                check = 1
                check = 0
                Range("AJ1").Activate
                Selection.AutoFilter
                Selection.End(xlToRight).Select
                Range("AJ1").Select
                ActiveSheet.Range("$A$1:$AQ$37518").AutoFilter Field:=36, Criteria1:= _
                    "=*Customer*", Operator:=xlAnd
                Exit For
                check = 0
            Else
            End If
        Next j
        If check = 1 Then
            Exit For
        Else
        End If
    Next i
End Sub
4

1 回答 1

3

这是冻结 excel,因为您通过循环遍历每一行(1048576),然后遍历该行中每个单元格中的每个字符来尽可能低效地进行比较。

相反,请尝试使用 range.Find方法:

Sub testFind()
Dim rng As Range
Dim rngFound As Range

Set rng = Range("AJ:AJ")

Set rngFound = rng.Find("CUSTOMER")

If rngFound Is Nothing Then
    MsgBox "Customer N/A"
Else:
    'Apply your filter here

End If


End Sub
于 2013-05-28T13:41:00.797 回答