VB to select 2 cells simultaneously

velohead

Board Regular
Joined
Aug 22, 2007
Messages
212
Hi All

No doubt the solution is quick, but here goes....

I want to select 2 non-contiguous cells within a macro.
If I specify….
Range("D5,M16").Select
…that works.

If I use the same logic and use the following code (for different cells) then it does not work.

Code:
ACA1 = ActiveCell.Address
ActiveCell.Offset(3, 3).Select
ACA2 = ActiveCell.Address

Sheets("Sheet2").Select
Range(ACA1, ACA2).Select

What am I missing please (ie what’s the solution).

Thanks All
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi,

To do what you were doing, the syntax would look like this:
Code:
    ACA1 = ActiveCell.Address
    ActiveCell.Offset(3, 3).Select
    ACA2 = ActiveCell.Address
    Sheets("Sheet2").Select
    Range("" & ACA1 & "," & ACA2 & "").Select

However, you might prefer this:
Code:
    Union(ActiveCell, ActiveCell.Offset(3, 3)).Select


Regards,
 
Last edited:
Upvote 0
Thanks RickXL, I like the union solution, quite elegant.

Seems a bit strange IMHO that
Code:
Range("" & ACA1 & "," & ACA2 & "").Select
works, but
Code:
Range(ACA1, ACA2).Select
does not work, as they are effectively the same thing.
 
Upvote 0
They are not quite the same thing.

The one that works has a single string to specify the range and the one that doesn't has two strings separated by commas. Subtle but crucial. ;)


regards,
 
Upvote 0
Seems a bit strange IMHO that
Code:
Range("" & ACA1 & "," & ACA2 & "").Select

Try ;
Code:
Range(ACA1 & "," & ACA2).Select


Union(ActiveCell, ActiveCell.Offset(3, 3)).Select

This works provided that Sheet2 is the active sheet at the start. If not :
Code:
Dim r$
r = Union(ActiveCell, ActiveCell.Offset(3,3).Address
Sheets("Sheet2").Select
Range(r).Select
 
Upvote 0
Yes, the
Code:
Range(ACA1 & "," & ACA2).Select
is a bit clearer and still works (as one string)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

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