Copy multiple ranges with VBA

后端 未结 2 1068
耶瑟儿~
耶瑟儿~ 2020-12-21 17:31

I am trying to copy multiple ranges using Excel VBA. I understand that in order to select multiple ranges, syntax similar to that below is used:

Range(\"A1:B         


        
相关标签:
2条回答
  • 2020-12-21 18:05

    Use the "Union" method.

    Dim range1 as Range, range2 as Range, multipleRangeAs Range    
    Set range1 = Sheets("Sheet1").Range("A1:B4000")    
    Set range2 = Sheets("Sheet1").Range("F1:F4000")    
    Set multipleRange= Union(range1, range2)
    

    Then you can mess around with mutipleRange as per normal.

    0 讨论(0)
  • 2020-12-21 18:07

    @Scott Holtzman provided the solution in a comment:

    I would just adjust the following, since the OP asked for dynamic range names, Set range1 = Range("A1:B" & Range("B" & Rows.Count).End(xlUp).Row) to get the real last cell in column B. Do the same for column F as well

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