将公式添加到引用范围的excel单元格中

时间:2015-01-26 02:45:58

标签: vba cell formula

在下面的代码中,我可以将公式的结果添加到单元格中,但是我需要单元格中的公式,所以当我在宏运行后更改范围的值时,该单元格的值会更新。

在下面的代码中,这是我遇到问题的行。

Cells(i,LastCol).Value = Application.WorksheetFunction.SumIf(Sourcerange,"< ="& Cells(2,2),FillRange)

 Set Sourcerange = Range("G3", Cells(3, LastCol))
    Range("G65536").End(xlUp).Select
    Set wf = Application.WorksheetFunction
    Set r = ActiveCell.Offset(0, LastCol)
    r.Select
    Set r = ActiveCell.Offset(0, -5)
    r.Select
    Set rAbove = Range(r, Cells(2, r.Column))
    RowCount = Range(r, Cells(2, r.Column)).Count
    LastCol = r.Column
    FillRange.Select
    For i = 6 To RowCount + 1
        Set FillRange = Range(Cells(i, 7), Cells(i, LastCol))
        FillRange.Select
        Cells(i, LastCol).Value = Application.WorksheetFunction.SumIf(Sourcerange, "<=" & Cells(2, 2), FillRange)
    Next i

谢谢! 布兰登

1 个答案:

答案 0 :(得分:2)

试试这个

Cells(i, LastCol).Formula = "=SUMIF(" & SourceRange.Address & ",""<=""&" & Cells(2, 2).Address & "," & FillRange.Address & ")"