VBA 到更改单元格引用的 FillDown 公式
VBA to FillDown Formula with changing cell reference
我有一个 Sumifs 函数要在具有 2 个条件的宏中完成。我的 excel 文件很大。所以我的代码看起来像这样:
Sub SumIfs()
ThisWorkbook.Sheets("Sheet1").Activate
Range("L2") = Application.WorksheetFunction.SUMifs(Range("G:G"),Range("A:A"),"Pen",Range("F:F"),"John")
End Sub
但我想将 "Pen" 更改为它的单元格引用,这意味着 "A2" 并且“John”对于 F:F 范围内的所有单元格保持不变。然后为下面的所有单元格填写公式。我使用了这段代码,
Application.WorksheetFunction.SUMifs(Range("G:G"),Range("A:A"), A2,Range("F:F"),"John")
但是当我执行 filldown 函数时,它只在单元格中显示 A2 down 的值。请帮我解决一下这个。提前致谢。
听起来你在寻找类似的东西:
Sub SumIfs()
With ThisWorkbook.Sheets("Sheet1")
.Range("L2:L" & .Range("G" & .Rows.Count).End(xlUp).Row).Formula = _
"=SUMIFS(G:G,A:A,A2,F:F,""John"")"
End With
End Sub
如果您想要 值,而不是 公式,插入到列 L,您可以将该代码扩展为:
Sub SumIfs()
Dim LastRow As Long
With ThisWorkbook.Sheets("Sheet1")
'Assume we want to create values for every cell in column L down until
'we get to the last cell in column G
LastRow = .Range("G" & .Rows.Count).End(xlUp).Row
'Paste the formula
.Range("L2:L" & LastRow).Formula = _
"=SUMIFS(G:G,A:A,A2,F:F,""John"")"
'Convert to values
.Range("L2:L" & LastRow).Value = _
.Range("L2:L" & LastRow).Value
End With
End Sub
我有一个 Sumifs 函数要在具有 2 个条件的宏中完成。我的 excel 文件很大。所以我的代码看起来像这样:
Sub SumIfs()
ThisWorkbook.Sheets("Sheet1").Activate
Range("L2") = Application.WorksheetFunction.SUMifs(Range("G:G"),Range("A:A"),"Pen",Range("F:F"),"John")
End Sub
但我想将 "Pen" 更改为它的单元格引用,这意味着 "A2" 并且“John”对于 F:F 范围内的所有单元格保持不变。然后为下面的所有单元格填写公式。我使用了这段代码,
Application.WorksheetFunction.SUMifs(Range("G:G"),Range("A:A"), A2,Range("F:F"),"John")
但是当我执行 filldown 函数时,它只在单元格中显示 A2 down 的值。请帮我解决一下这个。提前致谢。
听起来你在寻找类似的东西:
Sub SumIfs()
With ThisWorkbook.Sheets("Sheet1")
.Range("L2:L" & .Range("G" & .Rows.Count).End(xlUp).Row).Formula = _
"=SUMIFS(G:G,A:A,A2,F:F,""John"")"
End With
End Sub
如果您想要 值,而不是 公式,插入到列 L,您可以将该代码扩展为:
Sub SumIfs()
Dim LastRow As Long
With ThisWorkbook.Sheets("Sheet1")
'Assume we want to create values for every cell in column L down until
'we get to the last cell in column G
LastRow = .Range("G" & .Rows.Count).End(xlUp).Row
'Paste the formula
.Range("L2:L" & LastRow).Formula = _
"=SUMIFS(G:G,A:A,A2,F:F,""John"")"
'Convert to values
.Range("L2:L" & LastRow).Value = _
.Range("L2:L" & LastRow).Value
End With
End Sub