vba code line to select to the last row of data in 3 different columns

bh24524

Active Member
Joined
Dec 11, 2008
Messages
319
Office Version
  1. 2021
  2. 2007
Hi, as the title says, I'm trying to work thru some code to select data in 3 different columns which will ultimately be pasted onto a separate tab. I tried first just using ".select" after the ranges to see if the code was actually grabbing the correct info before I change that line to ".copy". I want to select A5 thru the last row of data of column B, and then from L5 thru the last column of Data in that same column. So columns A, B, and L. I tried:

VBA Code:
Range("A5:B5", Range("B5").End(xlDown) & Range("L5").End(xlDown)).Select

but am getting an error. I haven't had to use multiple non-consecutive ranges like that before in coding, so I can only try and guess at what it would be if it's actually doable. I am wondering what the codeline would be if it is or if you have to declare a variable?
 

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)
Something like this:
VBA Code:
Private Sub SelectRanges()
Dim bRow As Long, lRow As Long
bRow = Range("B" & Rows.Count).End(xlUp).Row
lRow = Range("L" & Rows.Count).End(xlUp).Row
Union(Range("A5:B" & bRow), Range("L5:L" & lRow)).Select
End Sub
 
Upvote 0
Solution
Something like this:
VBA Code:
Private Sub SelectRanges()
Dim bRow As Long, lRow As Long
bRow = Range("B" & Rows.Count).End(xlUp).Row
lRow = Range("L" & Rows.Count).End(xlUp).Row
Union(Range("A5:B" & bRow), Range("L5:L" & lRow)).Select
End Sub
Thank you Dreid, I hadn't known about the "Union" segment of code. With that I was actually able to do it without declaring a variable. I ended up simply with:

VBA Code:
Union(Range("A5:B5", Range("B5").End(xlDown)), Range("L5", Range("L5").End(xlDown))).Copy

Thanks for that piece of knowledge. I can see it being applicable in some other things I do :)
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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