运行时错误'1004'指定的值超出范围

时间:2013-09-10 10:03:40

标签: excel-vba runtime-error vba excel

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

您好,

我通过内部写的文本制作了上面的宏,用于在“蜷缩”的工作表中查找excel形状。宏可以在任何新书中使用,但不能在我需要的书中使用,如果它继续显示以下信息:

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

一旦点击“Debug”,它就会突出显示以下行:

sTemp = shp.TextFrame.Characters.Text

怎么了?

感谢您的帮助 Chiara的

3 个答案:

答案 0 :(得分:1)

您的代码没有任何问题。如果活动工作表受密码保护,则只会出现此错误。

你能检查一下吗?

同时查看以下网址

Excel macro "Run-time error '1004"

答案 1 :(得分:1)

我认为由于无法检查形状中是否存在 TextFrame ,因此应使用 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

`

答案 2 :(得分:1)

很抱歉打破常规,但我得到了类似的错误:

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

在我的场景中,我只是将一个形状作为GET属性的一部分返回到存储Shape对象的类中。该属性适用于Shape Type文本框,但在发送回形状时会缩小。如下所示。 我不能使用on错误,或者不知道怎么回事,因为错误发生在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