1

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?

DriveThomas
  • 35
  • 1
  • 8

2 Answers2

3

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...

Shai Rado
  • 33,032
  • 6
  • 29
  • 51
2
Columns(ColNumber).EntireColumn.Select

e.g.

Columns(1).EntireColumn.Select
Bentaye
  • 9,403
  • 5
  • 32
  • 45
Tbrook
  • 21
  • 3
  • 1
    not sure why you would use `Columns(ColNumber).EntireColumn.Select` and not `Columns(ColNumber).Select` ? – Shai Rado Mar 27 '18 at 11:34
  • Thank you for your answer, I wish I could select both of answers as correct, but Shai's is more complete so I went with his. – DriveThomas Mar 27 '18 at 11:50