VBA自动填充两列的最后一行

时间:2019-09-23 01:26:47

标签: excel vba

我想自动填充第4列和第5列中的公式,并假设我不知道哪一行包含最后一条数据。

**意识到我捏造了这个问题,做了一个小小的编辑。 TIA

https://i.stack.imgur.com/3m6SV.png

1 个答案:

答案 0 :(得分:0)

两个部分:

  1. 获取要复制的公式以及要填充的区域的参考
  2. 复制公式

查看内嵌评论

Sub Demo()
    Dim rFormulaToCopy As Range
    Dim FormulaColumn As Long, NumOfFormula As Long
    Dim DataColumn As Long

    '~~~ update to match your sheet layout
    FormulaColumn = 4
    NumOfFormula = 2
    DataColumn = 1

    With ActiveSheet '<~~~ specify the required sheet
        ' Reference the last row of Formula
        Set rFormulaToCopy = .Cells(.Rows.Count, FormulaColumn).End(xlUp).Resize(, NumOfFormula)
        ' Reference the missing formula range, including the last Formula row.
        ' Assign from/to the Formula property to copy formulas (Excel will update relative references)
        rFormulaToCopy.Resize(.Cells(.Rows.Count, DataColumn).End(xlUp).Row - rFormulaToCopy.Row + 1).Formula = rFormulaToCopy.Formula
    End With
End Sub