Excel – search形状中的文字
我想在Excel中search形状的文本,并在excel.tips.net中find以下代码
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
如果我search靠近工作表顶部的形状的单词,它将起作用。 然而,工作表是相当大的,如果我search中间或底部的东西,我会得到错误;
运行时错误“-2147024809(80070057)”:指定的值超出范围
我可以selectdebugging,这样做突出了代码行
sTemp = shp.TextFrame.Characters.Text
我正在使用Excel 2010。
感谢您的帮助,
Mattice
这不是一个答案(但很多的评论)
请尝试这一点,并检查是否仍然popup错误:
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
编辑
请试试看,并告诉我,如果错误popup:)
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