ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA to select multiple columns (https://www.excelbanter.com/excel-programming/312009-vba-select-multiple-columns.html)

Marvin

VBA to select multiple columns
 
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.

Norman Jones

VBA to select multiple columns
 
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



"Marvin" wrote in message
...
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.




MDW

VBA to select multiple columns
 
Try Range("C:E").Select

"Marvin" wrote:

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.


Norman Jones

VBA to select multiple columns
 
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

---
Regards,
Norman



"Marvin" wrote in message
...
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.




Don Guillett[_4_]

VBA to select multiple columns
 
one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub

--
Don Guillett
SalesAid Software

"Marvin" wrote in message
...
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.




Marvin

VBA to select multiple columns
 
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



"Marvin" wrote in message
...
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.



.


Norman Jones

VBA to select multiple columns
 
Hi Don,

Range(Cells(1, 1), Cells(1, 5)).Columns.Select



I think you intended:

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select

---
Regards,
Norman



"Don Guillett" wrote in message
...
one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub

--
Don Guillett
SalesAid Software

"Marvin" wrote in message
...
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.






MDW

VBA to select multiple columns
 
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

"MDW" wrote:

Try Range("C:E").Select

"Marvin" wrote:

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.


Don Guillett[_4_]

VBA to select multiple columns
 
Just tested. Both do the SAME and I like to keep em short.

--
Don Guillett
SalesAid Software

"Norman Jones" wrote in message
...
Hi Don,

Range(Cells(1, 1), Cells(1, 5)).Columns.Select



I think you intended:

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select

---
Regards,
Norman



"Don Guillett" wrote in message
...
one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub

--
Don Guillett
SalesAid Software

"Marvin" wrote in message
...
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.








Norman Jones

VBA to select multiple columns
 
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?

---
Regards,
Norman



"Don Guillett" wrote in message
...
Just tested. Both do the SAME and I like to keep em short.

--
Don Guillett
SalesAid Software

"Norman Jones" wrote in message
...
Hi Don,

Range(Cells(1, 1), Cells(1, 5)).Columns.Select



I think you intended:

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select

---
Regards,
Norman



"Don Guillett" wrote in message
...
one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub

--
Don Guillett
SalesAid Software

"Marvin" wrote in message
...
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.









Tom Ogilvy

VBA to select multiple columns
 
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


--
Regards,
Tom Ogilvy

"Don Guillett" wrote in message
...
Just tested. Both do the SAME and I like to keep em short.

--
Don Guillett
SalesAid Software

"Norman Jones" wrote in message
...
Hi Don,

Range(Cells(1, 1), Cells(1, 5)).Columns.Select



I think you intended:

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select

---
Regards,
Norman



"Don Guillett" wrote in message
...
one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub

--
Don Guillett
SalesAid Software

"Marvin" wrote in message
...
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.









Don Guillett[_4_]

VBA to select multiple columns
 
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

--
Don Guillett
SalesAid Software

"Norman Jones" wrote in message
...
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?

---
Regards,
Norman



"Don Guillett" wrote in message
...
Just tested. Both do the SAME and I like to keep em short.

--
Don Guillett
SalesAid Software

"Norman Jones" wrote in message
...
Hi Don,

Range(Cells(1, 1), Cells(1, 5)).Columns.Select


I think you intended:

Range(Cells(1, 1), Cells(1, 5)).entireColumn.Select

---
Regards,
Norman



"Don Guillett" wrote in message
...
one way using numbers.
Sub selectcolumns()
'Columns("a:e").Select
Range(Cells(1, 1), Cells(1, 5)).Columns.Select
End Sub

--
Don Guillett
SalesAid Software

"Marvin" wrote in message
...
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.












All times are GMT +1. The time now is 01:25 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com