Referencing a cell/range using a variable

dl7631

Board Regular
Joined
Mar 6, 2009
Messages
114
Hello!
I have a simple question.
I have introduced a variable FinalRow - let's say it's row 20.
Now, if I want to refer to a Range("A1:F20") using FinalRow - how should I do it? Will this work:
Range("A1".Offset(RowOffset:=FinalRow, ColumnOffset:=6)?

Thank you!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Are you trying to do this in VBA code I assume? If so, you would want something like:

Range("A1","F" & finalrow)

Also depending on how you have set final row, you may not need the F, but would need to see more of the code and how you declared finalrow?

Hope that helps.
 
Upvote 0
Try this.
Code:
Range("A1").Resize(FinalRow,6)
 
Upvote 0
Are you trying to do this in VBA code I assume? If so, you would want something like:

Range("A1","F" & finalrow)

Also depending on how you have set final row, you may not need the F, but would need to see more of the code and how you declared finalrow?

Hope that helps.

Yes, it's VBA.
Here is how I defined FinalRow:

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
 
Upvote 0
ADVERTISEMENT
Then what Norie and I have given should be sufficient, it could get rid of the F if the same number of rows is in F as in A by using:

Code:
FinalCell = Cells(Rows.Count, "F").End(xlUp)
Range("A1",FinalCell)
Hope that helps.
 
Upvote 0
schieln

I'm afraid that won't work - FinalCell will only contain value the of the last cell in column F.

If you want to take that approach try this.
Code:
FinalCell = Cells(Rows.Count, "F").End(xlUp).Address.
Or this.
Code:
Set FinalCell = Cells(Rows.Count, "F").End(xlUp)
Range("A1",FinalCell.Address)
 
Upvote 0

Forum statistics

Threads
1,196,329
Messages
6,014,678
Members
441,835
Latest member
rthomas268

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