• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Multiple, non-contiguous, column selection using column numbers

polarisking

Member
Say I want to do something with columns 2 through 5. Is there a way to specify Columns(2:5) or something similar?

What about a set of non-contiguous groups like 2-5, 7, 8-11, and 15? (can this be done with letters?)

Thanks in advance.
 
Hi polarisking
for contiguous columns you can use
Code:
Columns(3).Resize(, 3).Select
'columns 3 to 5

or
Code:
Range(Columns(3), Columns(5)).Select
For non contiguous columns
Code:
Union(Columns(3), Columns(5), Columns(7)).Select
 
With your example 2-5, 7, 8-11, and 15:
add some Cs in front, make those dashes colons leaving: C2:C5,C7,C8:C11,C15 (this is what R1C1 notation looks like where C means Column) then use it in the likes of, for example:
Range(Application.ConvertFormula("C2:C5,C7,C8:C11,C15", xlR1C1, xlA1)).Select
Check out the help on ConvertFormula, there's more to it.

Letter equivalent:
Range("B:E,G:G,H:K,O:O").Select
 
What does the Resize do?
Hi polarisking,
thanks for your feedback. The Resize property in Excel VBA makes a range a specific number of rows (0 in this case where the value is omitted) and columns (3) larger (or smaller if you use negative numbers). For a more detailed explanation see here
 
Hello everyone,
I apologize for writing something wrong, thanks to p45cal for pointing this out, as indicated in the linked article we cannot use negative row & column number for RESIZE property.
 
Back
Top