【发布时间】:2016-09-06 15:49:52
【问题描述】:
我对 VBA 很陌生,只有基本的知识水平。
我一直在尝试创建一个宏,以将一张工作表上的数据与同一工作簿中的多个其他工作表进行交叉引用。如果找到记录,我希望出现一个 msgbox,以提醒用户数据的位置。
在互联网上搜索了几个小时并拼凑出一些代码后,这就是我所拥有的
Sub search()
Dim ws As Worksheet, found As Range
Dim TextToFind(1 To 20) As String
Dim iText As Long
TextToFind(1) = "Jade Smith"
TextToFind(2) = "Bob Collins"
TextToFind(3) = "Jemima Smythe"
For Each ws In ThisWorkbook.Worksheets
With ws
If .Name <> "Blacklisted Candidates" Then 'Do not search blacklist candidates!
iText = 1
Do While iText <= UBound(TextToFind)
If TextToFind(iText) <> "" Then 'Do not search blank strings!
Set found = .UsedRange.Find(what:=TextToFind(iText), LookIn:=xlformulas, LookAt:=xlPart, MatchCase:=False)
If Not found Is Nothing Then
MsgBox "Proxy Candidate Found at " & found.Address
Else
MsgBox "No Proxy Candidates Found ", vbOKOnly, "Success!"
End If
iText = iText + 1
End If
Loop
End If
End With
Next ws
End Sub
但是,此代码无法从其他工作表中找到值。
在测试这个时,我只是在没有找到数据时得到 msgbox,即使那里有测试数据。
我有一个大约 9 张(不断增长)的工作簿,我想在每个工作簿的前 9 列中搜索指定的数据,如您所见,我已经手动输入宏,但是在运行宏时我即使有数据要查找,也不会返回任何结果。
【问题讨论】:
-
为什么末尾有“*”?
-
修改
*MsgBox ("Proxy Candidate Found at " And rngX.Address)为MsgBox "Proxy Candidate Found at " & rngX.Address -
您是否遗漏了一些代码? @litelite 有正确的答案。错误 13,但您的
Do循环无法退出。If Rng Is found Then也应该是If Not Rng Is Nothing Then。 -
@Comintern if 中有一个
goto,他用来跳出无限循环 -
@Comintern 是的,我读得很快。他的代码永远不会结束......
标签: vba excel runtime-error msgbox