Excel VBA Method 'Range' of object'_global' failed error 1004

前端 未结 1 502
眼角桃花
眼角桃花 2021-01-27 17:20

I can\'t for the life of my figure out why I\'m getting \"\'Range\' of object\'_global\' failed\" on my do while statement. It\'s like the range named RC3 isn\'t being recognize

相关标签:
1条回答
  • Do While (Range(RC3).Value <> "" And I < 30)
    

    RC3 is a Range object. 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.

    Note unqualified: your code implicitly works off the 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.

    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. 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.

    0 讨论(0)
提交回复
热议问题