Search code examples
excelvbaselectletter

Is there a way to select an entire column using the column number in VBA?


I want to select an entire column in Excel using VBA code, normally I'd do that like this Range("D:D").select

However in the current situation I only have the column numbers and I'd like to avoid having to convert the numbers into the corresponding letter. I have found a way to that here.

Can this be done?


Solution

  • You have a few options:

    1. Using .EntireColumn :

    Code

    Dim ColNumner As Long
    ColNumner = 5
    
    Cells(1, ColNumner).EntireColumn.Select
    
    1. Using directly Columns(ColNumner) :

    Code

    Dim ColNumner As Long
    ColNumner = 5
    
    Columns(ColNumner).Select
    

    Note: you should stay with from using Select. For instance, if you want to copy this column, you could use Columns(ColNumner).Copy, etc...