How to create a range from two ranges in VBA?

前端 未结 5 740
北恋
北恋 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:53

    Like this?

    Sub Sample()
        Dim rng1 As Range, rng2 As Range
        Dim NewRng As Range
    
        With ThisWorkbook.Sheets("Sheet1")
            Set rng1 = .Range("A1")
            Set rng2 = .Range("C3")
    
            Set NewRng = .Range(rng1.Address & ":" & rng2.Address)
    
            Debug.Print NewRng.Address
        End With
    End Sub
    

    Instead of R1C1 format use Cells(r,c). That will give you more flexibility + control

    So Range("A2") can be written as Cells(2,1)

提交回复
热议问题