Select range using cell reference (VBA)

ianto2842

New Member
Joined
Apr 7, 2021
Messages
19
Office Version
  1. 2013
Platform
  1. Windows
I am trying to get VBA to select a range of cells to copy based on a value in a given cell. The range always starts in the same place, same columns, but down to a particular row depending on the data. I cant use xlup as it isn't the last row and there are intentionally blank rows plus rows that appear blank but have formulae in them - so aren't seen as blank.

I need it to be able to select the area C3:J(value in B3) of the current sheet. Any help would be greatly appreciated! Using 2013.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Is the value in B3 unique?
Then you can use MATCH to find the row.
 
Upvote 0
Try this.
VBA Code:
Dim rng As Range
Dim LastRow As Long

    ' get last row from B3
    LastRow = Range("B3").Value

    ' set rng to refer to required range
    Set rng = Range("C3:J" & LastRow)

    ' do stuff with rng
    rng.Select

    rng.Copy
 
Upvote 0
Solution
Try this.
VBA Code:
Dim rng As Range
Dim LastRow As Long

    ' get last row from B3
    LastRow = Range("B3").Value

    ' set rng to refer to required range
    Set rng = Range("C3:J" & LastRow)

    ' do stuff with rng
    rng.Select

    rng.Copy
That's brilliant, does exactly what I need! Thank you :D
 
Upvote 0

Forum statistics

Threads
1,214,805
Messages
6,121,656
Members
449,045
Latest member
Marcus05

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