简体   繁体   English

对象'_global'的Excel VBA方法'Range'失败错误1004

[英]Excel VBA Method 'Range' of object'_global' failed error 1004

I can't for the life of my figure out why I'm getting "'Range' of object'_global' failed" on my do while statement. 我无法弄清楚为什么我的do while语句会出现“对象'_global'的'范围'失败”的原因。 It's like the range named RC3 isn't being recognized. 这就好像无法识别名为RC3的范围。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Sub DeleteBlankRows()

Dim Allrws As Range
Dim Rws As Range
Dim RC2 As Range
Dim RC3 As Range
Dim I As Integer
Dim CopyRange As Range
Dim LastRow As Long

With Application

'.ScreenUpdating = False ' don't spam the interface with selections

    With ActiveSheet

        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' Put the number of the last row in LastRow

    End With

    Set Allrws = Range("A2:S" & CStr(LastRow))  ' set range to be observed

    For Each Rws In Allrws.Rows   ' for each row in the range of Allrws call it Rws and do the following with it

        Set RC2 = Rws.Offset(0, 3).Resize(1, 1)      ' move and resize the range of RC2 to include only the cell under column D
        Set RC3 = Rws.Offset(0, 7).Resize(1, 1)      ' move and resize the range of RC3 to include only the cell under column I        
        I = 0   ' initilize the rows deleted counter

            Do While (Range(RC3).Value <> "" And I < 30)    ' as long as RC points to a empty cell and we haven't removed more then 30 rows keep removing rows                                                             

                If (range(RC2).Value = "Permit & Design" Or range(RC2).Value = "Miscellanious") Then 'don't delete row if Permit & Design or Miscellanious is in the cell under column D

                  I = 30        ' escape the loop if true

                Else

                  Selection.EntireRow.Delete      ' delete the selected row if false

                  I = I + 1                       ' add 1 to the counter

                End If        

            Loop                                   ' Go back to the start of the Do While

    Next Rws                    ' go back to the For Each and put the next row in Rws

    .ScreenUpdating = True      ' now update the interface with the changes

end with

end sub
Do While (Range(RC3).Value <> "" And I < 30)

RC3 is a Range object . RC3是一个Range对象 What Range(SomeRangeObject) does is really Range(SomeRangeObject.Value) , so unless RC3.Value contains a valid range address string, that unqualified Range call is going to blow up. Range(SomeRangeObject)所做的实际上是Range(SomeRangeObject.Value) ,因此,除非RC3.Value包含有效的范围地址字符串,否则不合格的 Range调用将被炸毁。

Note unqualified : your code implicitly works off the ActiveSheet : 注意不合格 :您的代码隐含地ActiveSheet

Set Allrws = Range("A2:S" & CStr(LastRow))

Whenever Range is used like this, it's implicitly doing ActiveSheet.Range , through the _Global hidden module. 每当使用Range这样时,它都会通过_Global隐藏模块隐式地执行ActiveSheet.Range

Unqualified Range , Cells , Rows , Columns and Names calls are all implicitly referring to the ActiveSheet , and the misunderstanding of this fact is the reason behind every single "Related" question in the side bar (the ones I checked anyway), and there are thousands more of the same on this site: it's an extremely common source of bugs. 不合格的RangeCellsRowsColumnsNames调用都隐式地引用ActiveSheet ,对此事实的误解是侧面栏中每个“ Related”问题(无论如何我都检查过)背后的原因,并且此站点上成千上万的同一个内容:这是错误的极其常见的来源。 So, qualify worksheet member calls and avoid problems. 因此, 限定工作表成员调用并避免出现问题。

Your code happens to work (well, given the above modification). 您的代码碰巧可以正常工作(嗯,鉴于上述修改)。 If the With ActiveSheet block was changed to With Sheet12 , you would start seeing issues stemming from all the unqualified Range calls. 如果将With ActiveSheet块更改为With Sheet12 ,您将开始看到源于所有不合格Range调用的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 vba excel:对象_global的运行时错误1004方法范围失败 - vba excel : runtime error 1004 method range of object _global failed “对象_global的运行时错误1004方法范围失败” VBA错误 - “runtime error 1004 method range of object _global failed” VBA error Excel VBA 运行时错误'1004'对象'_Global'的方法'范围'失败 - Excel VBA Run-Time Error '1004' Method 'Range' of object'_Global' Failed 损坏的Excel VBA宏运行时错误&#39;1004&#39;:对象&#39;_Global&#39;的方法&#39;Range&#39;失败 - Broken Excel VBA Macro Run-time error '1004': Method 'Range' of object '_Global' failed _Global的VBA错误1004对象范围失败 - VBA Error 1004 Object Range failed for _Global VBA运行时错误1004:尝试在Excel 2013中创建表时,对象_Global的方法范围失败 - VBA Run-time error 1004: Method Range of object _Global failed when trying to create tables in Excel 2013 Excel VBA:Excel中对象“ _Global”的方法“范围”失败 - Excel vba: Method 'Range' of object '_Global' failed error in excel 在带有命名范围的Excel中搜索时,object_global的运行时错误1004方法范围失败 - Run time error 1004 method range of object_global failed on a search in Excel with a named range 数据透视表的VBA错误:对象_Global的错误1004范围失败 - VBA Error with PivotTable: error 1004 range of object _Global failed VBA运行时错误1004 Object_Global的范围失败 - VBA Run Time Error 1004 Range of Object_Global Failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM