VBA FillDown公式更改单元格引用
我有一个Sumifs函数可以在2个标准的macros中完成。 我的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
但是我想将“笔”更改为其单元格引用,这意味着对于F:F范围内的所有单元格,“A2”和“John”保持不变。 然后填写下面所有单元格的公式。 我用这个代码,
Application.WorksheetFunction.SUMifs(Range("G:G"),Range("A:A"), A2,Range("F:F"),"John")
但是,当我做填充函数时,它只显示单元格中A2的值。 请帮我解决一下这个。 提前致谢。
这听起来像你是在类似的东西之后:
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