how to create a dynamic range in VBA

SKV

Active Member
Joined
Jan 7, 2009
Messages
257
I am developing a macro for auto-filling formulas for 3 cells for which row count can vary. So I want my macro to fill these formulas to all the required rows. Doing a macro recording I am getting a fixed length and I am NOT so good in VBA to play with the code.

Code by recording the macro -
Range("M2:O2").Select
Selection.AutoFill Destination:=Range("M2:O5598")
In the above code it will fill the to rows from 2 to 5598. I want this 5598 to be actually be row count.

Please advice what changes I need to make the code working
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Assuming that column A is populated try

Code:
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Range("M2:O2").AutoFill Destination:=Range("M2:O" & LR)
 
Upvote 0
Code:
With Range("M2:O2")

.AutoFill Destination:=.Resize(Range("A2").End(xlDown).Row)
End With
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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