jmaz
jmaz

Reputation: 527

What is the Correct Syntax of Variable as Column?

VBA will let me do this:

Columns("A:A").Select

But I need to do this:

Columns("ColNumToLet:ColNumToLet").Select

where ColNumToLet is a variable of type variant and has a value of A. VBA throws a Type mismatch error. What is the correct syntax?

Upvotes: 0

Views: 74

Answers (2)

Gary's Student
Gary's Student

Reputation: 96791

Here is how to use letters to define a span of columns:

Sub marine()
    alpha = "A"
    omega = "O"
    Columns(alpha & ":" & omega).Select
End Sub

Upvotes: 2

Tim Williams
Tim Williams

Reputation: 166835

No need to convert to letters:

 Columns(ColNum).Select

If you need to use letters then

 Columns(ColNumToLet).Select

will also work.

Upvotes: 2

Related Questions