VBA code for selecting multi cells (that are apart/not continuous)

ansvk1

Board Regular
Joined
Oct 6, 2017
Messages
82
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Hi there,
i am having trouble making my VBA code select the cells the way i wanted it to.

Here is what i wanted it to select
Code:
s2.Range("B2:M3,AB2:AB3").Select
at once to fill down the selection leaving the parts in middle. But my rowcount being dynamic, i have used variables and my code looks like
Code:
 s2.Range("B" & iLastRowS2 + 1 & ":M" & iLastRowS3, "AB" & iLastRowS2 + 1 & ":AB" & iLastRowS3).Select
this syntax is selecting everything from B2:AB3.

Now how can i tweak the syantax above to make use of my variables to select just the cells i wanted and not everything in between columns M and AB.

Any help is greatly appreciated, Thanks!
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Did you try creating a range string outside the range statement? If it still isn't working, you can uncomment the msgbox to see if the syntax looks right....

Code:
dim rg_str as String


rg_str = "B" & iLastRowS2 + 1 & ":M" & iLastRowS3, "AB" & iLastRowS2 + 1 & ":AB" & iLastRowS3

' msgbox(rg_str)


s2.Range(rg_str).Select
 
Upvote 0
no sir, i will try it now as you suggested and let you know. Thanks!
 
Upvote 0
rg_str = "B" & iLastRowS2 + 1 & ":M" & iLastRowS3, "AB" & iLastRowS2 + 1 & ":AB" & iLastRowS3

Ok, i tried it and found 2 things: (1) i will not need a msgbox as long as it tests out correctly for its selection. (2) rg_str ="B".... code is giving me a syntax error.

Please advise, how to get past these roadblocks. Thanks!
 
Last edited:
Upvote 0
Never mind, solved this problem with seperatly doing the selection one section after another. by selecting columns B:M once and then pasting the values for column AB using its dynamic values. Thanks for your Help!
 
Upvote 0
Here is what i wanted it to select
Code:
s2.Range("B2:M3,AB2:AB3").Select
at once to fill down the selection leaving the parts in middle. But my rowcount being dynamic, i have used variables and my code looks like
Code:
 s2.Range("B" & iLastRowS2 + 1 & ":M" & iLastRowS3, "AB" & iLastRowS2 + 1 & ":AB" & iLastRowS3).Select
this syntax is selecting everything from B2:AB3.
You were probably looking for this...
Code:
s2.Range("B" & iLastRowS2 + 1 & ":M" & iLastRowS3 & ",AB" & iLastRowS2 + 1 & ":AB" & iLastRowS3).Select
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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