填写公式到填充范围结束

时间:2014-08-28 13:51:26

标签: vba excel-vba automation excel

我想将我的公式复制到行的末尾 - 但我的语法似乎已关闭。

Columns("D:D").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("D3").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[1]=""low"",""1"",""0"")&IF(RC[2]=""LOW"",""1"",""0"")&IF(RC[3]=""Negative"",""1"",""0"")"
    Range("D2").Select
    ActiveCell.FormulaR1C1 = "Helper Column"
    Range("D3").Select
    **Selection.AutoFill Destination:=Selection.End(xlDown)**

1 个答案:

答案 0 :(得分:1)

您需要找到该列的最后一行,然后一次性输入公式。例如(UNTESTED)

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long
    Dim sFormula As String

    '~~> Change this to the relevant worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> Change this to the relevant formula
    sFormula = "=Sum(A1:C1)"

    With ws
        '~~> Find Last Row of Col D
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Enter the formula
        .Range("D3:D" & lRow).Formula = sFormula
    End With
End Sub

注意:如果Col B有定义最后一行的数据,请将.Range("A" & .Rows.Count).End(xlUp).Row更改为.Range("B" & .Rows.Count).End(xlUp).Row