VBA to select multiple columns

M

Marvin

I can use a statement like:

rows("3:5").select

I can use a statement like:

columns(3).select

but

columns("3:5").select

fails.

My objective is to select a number of columns based on a
cell value, starting with the current column.
Is it necessary for me to calculate the column "letters"
or is there a simpler way?

Thanks.
 
N

Norman Jones

Hi Marvin,

Dim Rng as range
set Rng = ActiveCell.EntireColumn.Resize(, 3)

It is rarely necessary to make selections, but if you do:

ActiveCell.EntireColumn.Resize(, 3).Select
 
N

Norman Jones

Hi Marvin,

I missed the cell value bit:

Sub Tester()
Dim Rng As Range
Dim i As Long

i = Range("A1").Value
Set Rng = ActiveCell.EntireColumn.Resize(, i)
Rng.Select
End Sub
 
D

Don Guillett

one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub
 
M

Marvin

Norman-

Thanks. Perfect solution.

-----Original Message-----
Hi Marvin,

Dim Rng as range
set Rng = ActiveCell.EntireColumn.Resize(, 3)

It is rarely necessary to make selections, but if you do:

ActiveCell.EntireColumn.Resize(, 3).Select

---
Regards,
Norman






.
 
G

Guest

Sorry, I didn't read the entire question.

I'll assume you know how many columns you want to select (and it's stored in
a variable called intNum), and you've got a range reference to the cell whose
value you want to check (called objRG).

If objRG.Value = "TheValueYouWant" Then

intStartCol = objRG.Column
intEndCol = intStartCol + intNum

For I = intStartCol To intEndCol

Columns(I).Select

Next

End If
 
N

Norman Jones

Hi Don,
Just tested. Both do the SAME and I like to keep em short

From the intermediate window:

Range(Cells(1, 1), Cells(1, 5)).Columns.Select
? Selection.address
$A$1:$E$1

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select
? Selection.Address
$A:$E

What was your test?
 
T

Tom Ogilvy

Not sure what you tested, but they are quite different in my tests:

Range(Cells(1, 1), Cells(1, 5)).Columns.Select
? selection.Address
$A$1:$E$1 ' 5 cells

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select
? selection.Address
$A:$E ' 5 columns
 
D

Don Guillett

Both of you are correct and I was wrong. I was in a hurry I guess I
commented wrong when testing.
..columns 'gets a1:e1
..entirecolumn 'of course, does the whole column
so solly
 

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