Excel VBA - select a dynamic cell range

前端 未结 4 1599
猫巷女王i
猫巷女王i 2020-12-13 11:08

I want to be able to dynamically select a range of cells (the heading row), where the row is 1 but the columns with be for 1 to last column, where \"A\" is the first Column

4条回答
  •  执笔经年
    2020-12-13 11:56

    So it depends on how you want to pick the incrementer, but this should work:

    Range("A1:" & Cells(1, i).Address).Select
    

    Where i is the variable that represents the column you want to select (1=A, 2=B, etc.). Do you want to do this by column letter instead? We can adjust if so :)

    If you want the beginning to be dynamic as well, you can try this:

    Sub SelectCols()
    
        Dim Col1 As Integer
        Dim Col2 As Integer
    
        Col1 = 2
        Col2 = 4
    
        Range(Cells(1, Col1), Cells(1, Col2)).Select
    
    End Sub
    

提交回复
热议问题