vba 运行时错误“1004”指定的值超出范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18715951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:15:24  来源:igfitidea点击:

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

excel-vbaruntime-errorvbaexcel

提问by ChiaraCiani

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:

我制作了上面的宏,用于通过里面写的文本在“crouded”工作表中查找excel形状。该宏适用于任何新书,但不适用于我需要的书,如果它一直显示以下消息:

"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

感谢您的帮助 Chiara

回答by JQuery Guru

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"

Excel 宏“运行时错误 '1004”

回答by Our Man in Bananas

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

我认为由于无法检查形状中是否存在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

`

`

回答by im_Spartacus

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?

在我的场景中,我只是在存储形状对象的类中返回一个形状作为 GET 属性的一部分。该属性适用于形状类型文本框,但在发送回线条形状时会失败。如下所示。我不能使用 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