Home > Net >  Drag formula to the last row (take first cell as A6)
Drag formula to the last row (take first cell as A6)

Time:05-24

I need to fill the formula till the last used row. I have data from A6:A28, however while using below code its dragging the formula till row 628. Issue is with 4th line of the code. Please help me correct.

Dim Lastrow As Long
Lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("H6:M6").Select
Selection.AutoFill Destination:=Range("H6:M6" & Lastrow), Type:=xlFillDefault

CodePudding user response:

Copy Down Formulas

A Quick Fix

  • Just remove the 6 after the M.
Selection.AutoFill Destination:=Range("H6:M" & LastRow), Type:=xlFillDefault

An Improvement

  • No Select
  • No variable
  • No AutoFill
With ActiveSheet ' improve!
    With .Range("H6:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        .Formula = .Rows(1).Formula
    End With
End With
  • Related