【问题标题】:Excel VBA with single search criteria, loop for all distinct values具有单一搜索条件的 Excel VBA,循环查找所有不同的值
【发布时间】:2015-04-04 20:54:39
【问题描述】:

我在运行宏时收到此错误消息:

运行时错误“6”:溢出

我有两个工作表;搜索和数据。 “数据”工作表包含两列,A 列包含我要搜索的数字,B 列包含一个字母数字值,当找到数字匹配项时,我想将其复制并粘贴到“搜索”工作表中。因为我正在搜索的数字可以列出未知次数我希望宏循环查找所有实例,将值复制到其右侧并将其粘贴到单元格 D3 中的“搜索”工作表中并继续找到多个实例的数字。

我正在搜索的号码位于“搜索”工作表的单元格 B3 中。

这是“数据”工作表的示例:

ID          ISS_ID
108143      136KQV4
108143      173HBK3
108143      136KQX0
109728      7805JM1
109706      7805JM1
102791      23252T4
105312      6477LZ6

这是我现在拥有的代码:

Sub Acct_Search()

    Dim searchResult As Range
    Dim x As Integer

    x = 3

    ' Search for "Activity" and store in Range
    Set searchResult =        Worksheets("Data").Range("A1:A3500").Find(What:=Worksheets("Search").Range("B3"), _
                     LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
                     SearchDirection:=xlNext, MatchCase:=False, _
                     SearchFormat:=False)

    ' Store the address of the first occurrence of this word
    firstAddress = searchResult.Address
    Do

        ' Set the value in the O column, using the row number and column number
        Worksheets("Search").Cells(x, 4) = searchResult.Offset(0, 1).Value

        ' Increase the counter to go to the next row
        x = x + 1

        ' Find the next occurrence of "Activity"
        Set searchResult = Cells.FindNext(searchResult)

        ' Check if a value was found and that it is not the first value found
    Loop While Not searchResult Is Nothing And firstAddress <> searchResult.Address

End Sub

当我调试它指向x = x + 1 行。现在它可以毫无问题地复制和粘贴第一个值,但是在那之后错误开始发挥作用。

【问题讨论】:

  • 您发布的代码与您提供的示例数据完美匹配。确切的错误信息是什么?您是否在“搜索”工作表的单元格 B3 中找到了要查找的 ID?
  • 确切的错误出现在 Microsoft Visual Basic 窗口中,并指出:“运行时错误 '6':溢出”。然后它让我可以选择结束、调试或帮助。如果我选择调试,它会突出显示 x = x + 1 行代码。在 B3 中搜索任何数字时遇到此错误。当我尝试使用 108143 时,它按预期粘贴了第一个值,但随后会弹出错误。

标签: vba excel


【解决方案1】:

您的问题发生了变化,因为您没有使用Range.FindNext MethodAfter:=... 参数重置搜索的原点。是的,您正在传递 searchResult,但它不接受它作为 After:= 参数。

当我运行您的代码时,由于 FindNext 总是找到相同的第二个实例,我陷入了无限循环。这解释了整数在超过 2¹⁵ 时会咳嗽。当它改为长时,这给了其他东西窒息的时间。

在我更改了一行以明确包含命名参数后,一切都清楚了。

  Set searchResult = Cells.FindNext(After:=searchResult)

这可以通过添加/删除参数名称来重现。似乎Cells.FindNext(searchResult) 正在查找 Search!B3,因为那不是 firstAddress,它只是在同一个 Search!B3 上循环。直到我强迫after:=searchResult .FindNext 自行调整。像这样的时候,我怀念我的 C/C++ 日子,没有这种沉闷的开销。

我已经检查了你的代码并添加了一个 With ... End With 块,它应该可以阻止任何可疑的出身。

Sub Acct_Search()

    Dim searchResult As Range, firstAddress As String
    Dim x As Long, ws As Worksheet

    x = 3
    Set ws = Worksheets("Search")

    ' Search for "Activity" and store in Range
    With Worksheets("Data").Range("A1:A3500")
        Set searchResult = .Find(What:=ws.Range("B3"), LookIn:=xlFormulas, After:=.Cells(.Rows.Count, .Columns.Count), _
                                 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                 MatchCase:=False, SearchFormat:=False)

        ' Store the address of the first occurrence of this word
        firstAddress = searchResult.Address
        Do
            ' Set the value in the O column, using the row number and column number
            ws.Cells(x, 4) = searchResult.Offset(0, 1).Value

            ' Increase the counter to go to the next row
            x = x + 1

            ' Find the next occurrence of "Activity"
            Set searchResult = .FindNext(After:=searchResult)
            'Debug.Print searchResult.Address(0, 0, external:=True)

            ' Check if a value was found and that it is not the first value found
        Loop While Not searchResult Is Nothing And firstAddress <> searchResult.Address
    End With

    Set ws = Nothing

End Sub

尽管不再需要 After:= 参数名称,但我保留了它。

【讨论】:

    【解决方案2】:

    改变

    Dim x As Integer
    

    Dim x As Long
    

    【讨论】:

    • 进行更改时出现不同的错误。它现在显示“运行时错误'1004':应用程序定义或对象定义的错误。
    • 我还应该补充一点,当我选择 Debug with this change 时,它​​会突出显示这行代码: Worksheets("Search").Cells(x, 4) = searchResult.Offset(0, 1 ).值
    • 您需要检查searchresult is nothing 。确保您确实找到了另一个结果。
    • 从上面的数据中,有多个 108143 实例,但错误仍然存​​在。从中我应该能够推断出搜索结果不应该是什么。当我需要它返回每个实例的值时,它仍然只返回第一个实例旁边的值。这是你指出的还是我错过了你的意思?
    • 不确定您现在遇到错误的确切原因。很难在手机屏幕上分析。但是,您将需要检查 not found 因为您最终会找到它
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多