Selecting cells by variable

Deadly48

New Member
Joined
Jun 14, 2006
Messages
19
Dim Var3, Var6 As Integer

Var3 = 3
Var6 = 6
'
Range("A" & Var3 & ":" & "X" & Var6).Select

The above code selects cells in a range A3 to X6

How can I modify to select a group of individual cells ie. A3, B7, C10 where 3,7,10 are the variables.

Regards
Deadly48
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi Deadly48

Please try:

Code:
Sub SelectCells()

Dim Var3 As Integer, Var7 As Integer, Var10 As Integer

Var3 = 3
Var7 = 7
Var10 = 10

Application.Union(Range("A" & Var3), Range("B" & Var7), Range("C" & Var10)).Select
End Sub

Remark: Selecting in usually a bad programming practice. Most of the times it's unnecessary and inefficient. Can you not just refer to the range?

Hope this helps
PGC
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
P. S. In your post you had

Dim Var3, Var6 As Integer

This declares Var3 as Variant and Var6 as Integer. if you want both as integer:

Dim Var3 As Integer , Var6 As Integer
 

Forum statistics

Threads
1,137,201
Messages
5,680,136
Members
419,885
Latest member
Murugesha

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
Top