Excel select columns numerically in VBA

https://www.excelcampus.com/library/vba-reference-column-numbers/comment-page-1/#comment-84622

I used a variant of this to select columns for sorting:
   lastcol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
   ws.Columns(1).Resize(, lastcol).Select

Thank you Mr.  Jon Acampora !:
Sub Column_Number_References()
'This macro contains VBA examples of how to
'reference or select a column by numbers instead of letters
    
Dim lCol As Long
    
    'The standard way to reference columns by letters
    Range("A:B").Select
    Range("A:A").Select
    Columns("A").Select
    Columns("A:F").Select
     
    'Select a single column with Columns property
    'The following line selects column B (column 2)
    Columns(2).Select
     
    'Select columns based on a variable
    lCol = 2
    Columns(lCol).Select
     
    'Select columns based on variable of a cell's value
    'Cell A1 must contain a valid column number.
    lCol = Range("A1").Value
    Columns(lCol).Select
     
    'Select multiple columns with the Range and Columns properties
    'The following line selects columns B:E
    Range(Columns(2), Columns(5)).Select
     
    'Select multiple columns with the Columns and Resize properties
    'The following line selects columns B:D. 
    'It starts a column 2 and resizes the range by 3 columns.
    'The row parameter of the resize property is left blank.  
    'This is an optional parameter (argument) and is
    'not required for the column resize.
    Columns(2).Resize(, 3).Select

End Sub

Comments

Popular posts from this blog

Powerpoint countdown and current time in slides VBA

Revit area plans adding new types and references (Gross and rentable)