将公式拖到最后一行(取第一个单元格为A6)

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

我需要将公式填到最后使用的行。我有来自 A6:A28 的数据,但是在使用下面的代码时,它会将公式拖到第 628 行。问题出在代码的第 4 行。请帮我更正。

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

复制公式

快速修复

  • 只需删除 M 之后的 6
Selection.AutoFill Destination:=Range("H6:M" & LastRow), Type:=xlFillDefault

一个改进

  • 没有Select
  • 无变量
  • 没有AutoFill
With ActiveSheet ' improve!
    With .Range("H6:M" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        .Formula = .Rows(1).Formula
    End With
End With