VBA - Select columns using numbers?

前端 未结 9 2184
北荒
北荒 2021-02-05 08:06

I\'m looking for an alternative to this code, but using numbers. I want to select 5 columns, the start column is a variable, and then it selects 5 columns from this

9条回答
  •  执笔经年
    2021-02-05 08:19

    I was looking for a similar thing. My problem was to find the last column based on row 5 and then select 3 columns before including the last column.

    Dim lColumn As Long
    
    lColumn = ActiveSheet.Cells(5,Columns.Count).End(xlToLeft).Column
    MsgBox ("The last used column is: " & lColumn)
    Range(Columns(lColumn - 3), Columns(lColumn)).Select
    

    Message box is optional as it is more of a control check. If you want to select the columns after the last column then you simply reverse the range selection

    Dim lColumn As Long
    
    lColumn = ActiveSheet.Cells(5,Columns.Count).End(xlToLeft).Column
    MsgBox ("The last used column is: " & lColumn)
    Range(Columns(lColumn), Columns(lColumn + 3)).Select
    

提交回复
热议问题