陷入无限循环search文本框

我的代码search整个工作表,search与input的短语匹配的文本框。 按是则转到下一个响应,使用该短语否将复制当前文本框。 我的问题是它没有search整个工作表,但卡住循环通过一个工作表,直到我按否复制。 我是新来的VBA,所以请任何帮助将感恩。

Sub FindResponse() Dim rStart As Range Dim shp As Shape Dim sFind As String Dim sTemp As String Dim Response Dim obj As New DataObject Dim ws As Worksheet sFind = InputBox("Search for?") If Trim(sFind) = "" Then MsgBox "Nothing entered" Exit Sub End If Set rStart = ActiveCell For Each ws In Worksheets For Each shp In ActiveSheet.Shapes With shp sTemp = shp.TextFrame.Characters.Text If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then shp.Select Response = MsgBox( _ prompt:=shp.Name & vbCrLf & _ sTemp & vbCrLf & vbCrLf & _ "Yes to see other matches: No to copy text", _ Buttons:=vbYesNo, Title:="Continue?") If Response <> vbYes Then obj.SetText sTemp obj.PutInClipboard Exit Sub End If End If End With Next shp Next ws On Error GoTo 0 MsgBox "Value not found" End Sub 

你的循环的结构是这样的:

 For Each ws In Worksheets For Each shp In ActiveSheet.Shapes With shp ... End With Next shp Next ws 

正如你可能在仔细观察后所猜到的,它留在同一张纸上的原因是在你的内部循环中,你循环了ActiveSheet.Shapes ,而不是ws.Shapes ,我怀疑你打算做什么。

听到这个循环是无限的,我感到有点惊讶 – 它是否可以循环调用工作表中相同次数的工作表?