Excel VBA: changing hard coded column to dynamic range to autofill to last row

Yucca

New Member
Joined
Feb 24, 2014
Messages
2
Hello from an unexperienced vba user.. I'm having trouble with the code for autofill to the last row when trying to use named ranges for the column. Everything seems to work fine when I use a hard coding for the column, in this case Column CW, and what I need is to replace this column CW with a named range so that the macro will still work when adding or deleting columns in the worksheet.

I used the following named ranges:

First_Date: This is the header cell of one of the columns (in this case AP5)
Second_Row: This is the range of columns I want to copy the formulas from (AP7:CW7)
Second_Cell: The cell where I want to start to autofill (AP7)
Last_Column: This is column CW that I want to use in the code. Autofill would be up to this column and down to the last row.

After searching in different threads, I came up with the following code that seems to work fine. How can I change column CW to a named range? Or do I need to change the code?

Code:
Dim Lr As Integer 

Lr = Range("First_Date").End(xlDown).Row 
'Searching last row  
Rows(Lr).Insert Shift:=xlDown 
'Inserting new row 
Range("Second_Row").AutoFill Destination:=Range(Range("Second_Cell"), Range("CW" & Lr))

Can anyone assist me here please?
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Change this...
Range("CW" & Lr)

To This...
Cells(Lr, Range("Last_Column").Column)

Edit:
Also, you could find the last column just as you found the last row.
Lc = Cells(7, Columns.Count).End(xlToLeft).Column
 
Last edited:
Upvote 0
Thanks a million! I used the first option and it did the job, since I don't know how I would have to use Lc in this code.. What does the 7 stand for and how would I use it in the autofill code?
 
Upvote 0
You're welcome.

When your in the VBA editor, you can highlight a command e.g. Cells, press F1 and you will get its' help; description, syntax and an example code.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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