excel - Sumif 跨两个工作表
问题描述
我正在尝试通过 sumif 将各个项目的各种员工成本(包含在名为“时间表条目”的单独工作表中)相加(即,如果左侧栏中的项目编号与时间表条目匹配,则将我所有的员工加起来成本,然后做下一个项目,依此类推。
代码几乎可以工作了!但返回 0 作为每个项目的总和。
With Worksheets("Cost Data Summary")
Lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To Lastrow
.Cells(i, 2).Value = Application.SumIf(Worksheets("Timesheet Entry").Range("A2:F50"), "=.Cells.Value(i, 2)", Worksheets("Timesheet Entry").Range("F2:F50"))
Next i
End With
我只能假设我以某种方式错误地引用了标准。我希望它引用 sumif 答案所在左侧单元格中的文本。
任何帮助将不胜感激。
解决方案
Try replacing
"=.Cells.Value(i, 2)"
with
.Cells.Value(i, 2)
When enclosed in quotes, the code turns into a string literal. You shouldn't need the = sign for the argument.