简体   繁体   中英

How to select a fixed number of adjacent columns starting from N column in excel using VBA

I have a worksheet with about 50 columns of products starting from column "T"

and I want to select columns from Column "T" and 40 columns on the right but I am confused how can I achieve this

should I rely on something like

Columns("T:(number of columns)").Select 

Try this ...

Public Sub SelectColumnsWithStartAndOffset()
    Dim strFirstColumn As String, lngOffset As Long, strLastColumn As String

    strFirstColumn = "T"
    lngOffset = 40

    With Sheet1
        strLastColumn = Split(.Range(strFirstColumn & "1").Offset(0, lngOffset).Address, "$")(1)
        .Range(strFirstColumn & ":" & strLastColumn).Select
    End With
End Sub

You may just need to adjust the offset 1 (give or take) to make sure the correct amount of columns are selected.

I hope that works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM