Create excel ranges using column numbers in vba?

前端 未结 9 2126
误落风尘
误落风尘 2021-02-02 05:56

How is it possible to create a range in vba using the column number, rather than letter?

9条回答
  •  执念已碎
    2021-02-02 06:20

    To reference range of cells you can use Range(Cell1,Cell2), sample:

    Sub RangeTest()
      Dim testRange As Range
      Dim targetWorksheet As Worksheet
      
      Set targetWorksheet = Worksheets("MySheetName")
      
      With targetWorksheet
        .Cells(5, 10).Select 'selects cell J5 on targetWorksheet
        Set testRange = .Range(.Cells(5, 5), .Cells(10, 10))
      End With
      
      testRange.Select 'selects range of cells E5:J10 on targetWorksheet
      
    End Sub
    

    enter image description here

提交回复
热议问题