我想在Excel上搜索形状文本,并在excel.tips.net
中找到以下代码Sub FindInShape1()
Dim rStart As Range
Dim shp As Shape
Dim sFind As String
Dim sTemp As String
Dim Response
sFind = InputBox("Search for?")
If Trim(sFind) = "" Then
MsgBox "Nothing entered"
Exit Sub
End If
Set rStart = ActiveCell
For Each shp In ActiveSheet.Shapes
sTemp = shp.TextFrame.Characters.Text
If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then
shp.Select
Response = MsgBox( _
prompt:=shp.Name & vbCrLf & _
sTemp & vbCrLf & vbCrLf & _
"Do you want to continue?", _
Buttons:=vbYesNo, Title:="Continue?")
If Response <> vbYes Then
Set rStart = Nothing
Exit Sub
End If
End If
Next
MsgBox "No more found"
rStart.Select
Set rStart = Nothing
End Sub
如果我搜索工作表顶部附近形状的单词,它会起作用。 但是,工作表非常大,如果我在中间或底部搜索某些内容,我会收到错误;
运行时错误&#39; -2147024809(80070057)&#39;: 指定的值超出范围
我可以选择调试,这样做会突出显示代码行
sTemp = shp.TextFrame.Characters.Text
我正在使用Excel 2010。
感谢您的帮助,
马蒂斯
答案 0 :(得分:2)
这不是一个答案(但很多评论)
请尝试此操作并检查错误是否仍然弹出:
Sub testForError()
Dim shp As Shape, i As Long
On Error Resume Next
For Each shp In ActiveSheet.Shapes
i = i + 1
Debug.Print i & " " & shp.Type
Debug.Print i & " " & shp.TextFrame.Characters.Text
Debug.Print i & " " & shp.TextFrame2.TextRange.Text
Next
Debug.Print "finished"
End Sub
修改强>
请尝试并告诉我是否弹出错误:)
Sub FindInShape1()
Dim shp As Shape
Dim sFind As String
Dim sTemp As String
sFind = InputBox("Search for?")
If Trim(sFind) = "" Then MsgBox "Nothing entered": Exit Sub
On Error Resume Next
For Each shp In ActiveSheet.Shapes
Debug.Print shp.TopLeftCell.Address
sTemp = ""
sTemp = shp.TextFrame.Characters.Text
If Len(sTemp) Then
If InStr(1, sTemp, sFind, 1) Then
shp.Select
If MsgBox(shp.Name & vbCrLf & sTemp & vbCrLf & vbCrLf & "Do you want to continue?", vbYesNo, "Continue?") <> vbYes Then Exit Sub
End If
End If
Next
MsgBox "No more found"
End Sub
答案 1 :(得分:0)
您忘记了在分配任何形状变量之前必须先放置“ set”。
Set sTemp = shp.TextFrame.Characters.Text