Select a number of cells - Rookie mistake

GiraffetheGeek

Board Regular
Joined
May 25, 2011
Messages
58
Hi all,

I must be brain dead today.

I am trying to get VB to select a number of cells based on the variable myline.

My code is:
Code:
Range("B" & myline, "E" & myline, "H" & myline, "K" & myline, "N" & myline).Select

I get the error message "Compile error: Wrong number of arguments or invalid property assignment"

I have drawn a blank. I know I've done this before but cannot remember how!

Also while I am here can anyone suggest a more efficient way of doing this if I need to increase the number of cells, ie increase from B,E,H,K,N on the same line to maybe 20 different cells on the same line?

Cheers in advance
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
This will work but it might be an idea to try some other way to select the cells, especially if you there are going to be 20 cells or more.
Code:
Range("B" & myline & ",E" & myline & ", H" & myline & ",K" & myline & ",N" & myline).Select
PS You probably don't need to select, depends on what you are doing next with the selection.
 
Upvote 0
One way:

Code:
    Dim iRow As Long
 
    iRow = 3
    Rows(iRow).Range("A1,E1,H1,K1,N1").Select
 
Upvote 0
@ shg,

Can you explain how you code works? in my example the variable myline would change, depending on the line selected by the user.

So being a rookie I don't really follow how your code fits with this.

Appreciate the input tho'
 
Upvote 0
One way:

Code:
    Dim iRow As Long
 
    iRow = 3
    Rows(iRow).Range("A1,E1,H1,K1,N1").Select

Really nice one!

I usually use (or should I say, used until now :)):

Code:
    iRow = 3
    Intersect(Rows(iRow), Range("A1,E1,H1,K1,N1").EntireColumn).Select

but yours is much better.
 
Last edited:
Upvote 0
Thank you, pgc, Giraffe.

Can you explain how you code works? in my example the variable myline would change, depending on the line selected by the user.
Any range has a range property (ad infinitum), and each range is taken relative to the range higher in the hierarchy.

In the example, the value of iRow is set in the code, but it could equally be taken based on the current selection:

Code:
Rows(Selection(1).EntireRow).Range("A1,E1,H1,K1,N1").Select
 
Last edited:
Upvote 0
Sorry, that last is bogus.

Code:
Selection(1).EntireRow.Range("A1,E1,H1,K1,N1").Select
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,749
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top