【发布时间】:2020-07-08 14:19:57
【问题描述】:
我想在 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
如果我搜索靠近工作表顶部形状的单词,它会起作用。 但是,工作表很大,如果我在中间或底部搜索某些东西,就会出错;
运行时错误“-2147024809 (80070057)”:指定的值超出 范围
我可以选择调试,这样做会突出显示代码行
sTemp = shp.TextFrame.Characters.Text
我使用的是 Excel 2010。
感谢您的帮助,
马蒂斯
【问题讨论】:
-
嗯,每个形状都包含文字吗?
-
@findwindow
shp.TextFrame.Characters.Text将导致大多数形状的错误...没有On Error Resume Next我不确定如何在没有错误消息的情况下运行它 -
我尝试将 shp.TextFrame.Characters.Text 更改为 shp.TextEffect.Text 但没有成功