0

I have recorded a macro in Excel, but when I run it with updated data, it only applies to the number of rows I had when I first had the macro recorded. I dove into the macro code and I think these are the problems:

Selection.AutoFill Destination:=Range("L2:R2"), Type:=xlFillDefault
    Range("L2:R2").Select
    Selection.AutoFill Destination:=Range("L2:R242")
    Range("L2:R242").Select

How can I adjust the macro to have the range set up as the size of the worksheet?

byrak79
  • 61
  • 6
  • You could [find the last row](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba). – BigBen Mar 08 '21 at 14:35

1 Answers1

0

Save the last row of your data into a variable and then modifiy the line to always fill the formula to that value like this:

Dim Last_Row as Long
Last_Row = Application.ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Range("L2:R2").AutoFill Destination:=Range("L2:R" & Last_Row), Type:=xlFillDefault

(Rows.Count, 1) this 1 represents the Column A. I supposed your data is here

David Donayo
  • 117
  • 9