PDA

View Full Version : [SOLVED] How to define range based on the value of another cell



BlueDolphin9
10-25-2019, 04:38 AM
Is it possible to define a range in VBA based on a value given in a cell?
For example, I have a dataset with a constantly changing number of rows. I have the number of rows in cell B2. Suppose cell B2 indicates the number of rows is 33, then I want the range to be D5: D33, but I want the selection of the last cell to be dependent on cell B2.
So if I change B2 to 50, the next time I run the macro, D5: D50 will be selected, but since I'm new to VBA I can't figure it out.

Thanks in advance for your help :)

mana
10-25-2019, 05:04 AM
Sub test()
Dim lastrow As Long

lastrow = Range("B2").Value
Range("D5:D" & lastrow).Select

End Sub

BlueDolphin9
10-25-2019, 06:59 AM
Perfect, Thank you :)