【发布时间】: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 时,它按预期粘贴了第一个值,但随后会弹出错误。