select non contiguos columns by variable name

J

John Keith

I have integer variables that define several columns of interest in a
worksheet. How do I select multiple non contiguous columns?

Example

parts = 3 ' column 3 has parts info
dates = 6 ' column 6 has date info
cost = 10 ' column 10 has cost info

columns(parts, dates, cost).Select this statement does not work

TIA


John Keith
(e-mail address removed)
 
M

michdenis

Hi,

Try this :

Range("parts, dates, cost").Select




"John Keith" <[email protected]> a écrit dans le message de groupe de discussion :
(e-mail address removed)...
I have integer variables that define several columns of interest in a
worksheet. How do I select multiple non contiguous columns?

Example

parts = 3 ' column 3 has parts info
dates = 6 ' column 6 has date info
cost = 10 ' column 10 has cost info

columns(parts, dates, cost).Select this statement does not work

TIA


John Keith
(e-mail address removed)
 
D

Dave Peterson

With ActiveSheet
Union(.Columns(parts), .Columns(dates), .Columns(cost)).Select
End With

But remember, it's very rare where you actually have to select a range to work
with it.
 
J

John Keith

With ActiveSheet
Union(.Columns(parts), .Columns(dates), .Columns(cost)).Select
End With

But remember, it's very rare where you actually have to select a range to work
with it.

Dave,

Thank you that worked perfectly. And yes, I was able to merge the next
line that began Selection.

What are the pros/cons to using Select?


John Keith
(e-mail address removed)
 
D

Dave Peterson

The cons are that you have to select something to work with it. The code is
difficult to read/modify.

The pros are that you can use what the macro recorder gave you. But that
recorded macro is probably very messy to understand.
 
M

michdenis

| I did try that and it did not work. But thank you for looking.

Range("parts, dates, cost").Select

*** it works if "parts", "dates" and "cost" are names not variables
representing a column number as it was the case in your question.
I simply misinterpreted your request !




John Keith
(e-mail address removed)
 
J

John Keith

| I did try that and it did not work. But thank you for looking.

Range("parts, dates, cost").Select

*** it works if "parts", "dates" and "cost" are names not variables
representing a column number as it was the case in your question.
I simply misinterpreted your request !

Ah, thank you. Someday I'm going to use range names and I'll add this
to my bag of tricks.


John Keith
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top