How to create a range from two ranges in VBA?

前端 未结 5 747
北恋
北恋 2020-11-29 09:15

I have two ranges, each containing a single cell (for example \"A1\" and \"C3\").

How do I get a new range containing all the cells between these two (\"A1:C3\")?

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 09:41

    Method 4 is not the same as Method 1 when the ranges are not adjacent.

    With Sheet1
    Set rng1 = .Range("A1:A3")
    Set rng2 = .Range("C1:C3")
    
    'This combines the two separate ranges, so select A1, A2, A3, C1, C2, C3
    set newRng = Union(rng1, rng2)
    
    'This combines the two ranges in the same way as when using "A1:C3", 
    'so including the cells from column B
    set newRng = .Range(rng1, rng2)
    

提交回复
热议问题