Excel VBA selecting multiple dynamic ranges

前端 未结 2 599
我寻月下人不归
我寻月下人不归 2020-12-19 14:24

I\'m trying to selecting multiple dynamic range. Trying to use the union method and I\'m getting Method \'Range\' of \'object\' Global Failed error on first Set line.

<
2条回答
  •  鱼传尺愫
    2020-12-19 14:35

    The problem occurs because of the comma in you range statements. I.e. when you set R1 you should write:

    Set R1 = Range("A7:A" & LR)
    

    Also, when you define the object type of your variables R1, ..., R5 you should write it as

    Dim R1 As Range, R2 As Range, R3 As Range, R4 As Range, R5 As Range, MultiRange As Range
    

    Otherwise R1, ..., R5 will be defined as a Variant. This doesn't cause a problem, but it will save memory and makes for a cleaner code.

提交回复
热议问题