Copy down formulas when new row is added

RockEd

Board Regular
Joined
Aug 13, 2021
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hello, I have a data entry userform that is supposed to enter data into the sheet, and then copy down formulas from the respective columns. The problem I seem to have is that it is copying down the formula's to roughly 200 further than the "bottom" - is there anything particularly obviously wrong with my code?

Note - I've taken out other code that was inputting the data into the sheet as it isn't relevant

VBA Code:
Private Sub writedatatosheet()

    Dim lastrow As Integer

 lastrow = Cells(Rows.Count, "A").End(xlUp).row
 
 With ActiveSheet
 .Range("AD2:AZ2").AutoFill Destination:=.Range("AD2:AZ2" & lastrow)
 End With

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You might have already done this, but, I'd check the obvious first- is there hidden data in those 200 "Empty" Rows? Manually Delete those Rows, save the workbook, Close it, then re-open it and run your code again.
 
Upvote 0
Perhaps:

VBA Code:
 .Range("AD2:AZ2").AutoFill Destination:=.Range("AD2:AZ2" & lastrow)

Should be:

VBA Code:
 .Range("AD2:AZ2").AutoFill Destination:=.Range("AD2:AZ" & lastrow)
 
Upvote 0
It's copying down too far, because this .Range("AD2:AZ2" & lastrow) is concatenating the value of lastrow with AZ2, so if lastrow is 100 you will get AZ2100
Try
VBA Code:
With ActiveSheet
 .Range("AD2:AZ2").Resize(lastrow-1).FillDown
 End With
 
Upvote 0
Solution
Thank you @Fluff ; I've decided to change the code slightly to only copy down columns that contain formulas. I've 'named' each cell (is there a correct way of saying this?) so that if users add columns then the code will still work.

Rather than typing (or copy / pasting) the line of code for each named range - can I create an array here?

I'm quite inexperienced with array's though.

Here is what I will have to copy multiple times:

VBA Code:
With ActiveSheet
 .Range("Col_Fee").Offset(1, 0).Resize(lastrow - 1).FillDown
 End With

Or my attempt at building an array but failing (getting compile error: For each control variable must be variant):

VBA Code:
Dim myarray() As Variant
Dim x As Long

myarray = Array("Col_Fee", "Col_Guaranteed_Stats") '(and continuing for the rest of the named cells)


For Each x In myarray
With ActiveSheet
 .Range(myarray(x)).Offset(1, 0).Resize(lastrow - 1).FillDown

 End With

Next x

Happy to post this as a new question if you'd prefer...
 
Upvote 0
It needs to be like
VBA Code:
For x = 0 To UBound(myarray)
With ActiveSheet
 .Range(myarray(x)).Offset(1, 0).Resize(lastrow - 1).FillDown

 End With
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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