Run-Time error '1004' The specified value

2020-05-04 22:40发布

Sub FindInShapes1()   
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.TopLeftCell & 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

Hi,

I made the above Macro for finding excel shapes in a "crouded" worksheet, by the text written inside. The macro works in any new books but not in the one I need, were it keeps on showing the following message:

"Run-Time error '1004'
The specified value is out of range"

and as soon as i click on "Debug" it highlights the line:

sTemp = shp.TextFrame.Characters.Text

What's wrong?

Thanks for your help Chiara

3条回答
疯言疯语
2楼-- · 2020-05-04 23:11

Sorry to break the convention but the similar error I get:

The specified value is out of range
Run-time error -2147024809

In my scenario I am simply returning a shape as part of a GET property in side a class that store a Shape Object. The property works for Shape Type Text Boxes but craps out on sending back Line Shapes. As per below. I cannot use the on error, Or don't know how because the error occur at End Property?

Public Property Get shp_Obj() As Shape
    If prvt_int_Ordinal = 13 Them

        MsgBox prvt_Shp_Shape.Name, , "prvt_Shp_Shape.Name"



    Set shp_Obj = prvt_Shp_Shape
   End If

End Property
查看更多
SAY GOODBYE
3楼-- · 2020-05-04 23:14

I think as there is no way to check for the existence of a TextFrame within a shape, you should ignore the error by using On Error Resume Next:

Sub FindInShapes1()
Dim rStart As Range
Dim shp As Shape
Dim sFind As String
Dim sTemp As String
Dim Response

On Error Resume Next

sFind = InputBox("Search for?")
If Trim(sFind) = "" Then
    MsgBox "Nothing entered"
    Exit Sub
    End If
    Set rStart = ActiveCell
    For Each shp In ActiveSheet.Shapes
        'If shp.TextFrame.Characters.Count > 0 Then
        If InStr(LCase(sTemp), LCase(sFind)) <> 0 Then
            shp.Select
            Response = MsgBox( _
                prompt:=shp.TopLeftCell & 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
        'End If
        sTemp = shp.TextFrame.Characters.Text

    Next
    MsgBox "No more found"
    rStart.Select
    Set rStart = Nothing
End Sub

`

查看更多
Lonely孤独者°
4楼-- · 2020-05-04 23:20

There is nothing wrong with your code. You will only get this error if the Active worksheet is password protected.

Can you check that?

Also check below url from so

Excel macro "Run-time error '1004"

查看更多
登录 后发表回答