我想知道是否有人可以帮助我。
我已经尝试了大约一个星期的时间来整理一张包含基于多重标准的'SumIf'公式的表格。
我已经能够这样做,在单元格范围内手动输入具有所需结果的公式。
我遇到的问题实际上是双重的。
为了更好地说明这一点,我附上了一个文件here,显示了我想要实现的目标。
对于每个单元格,我想执行以下'SumIf':
正如我所说,我已经做了一段时间了,但我无法想出解决方案。
我只是想知道是否有人可能会看到这个,并就如何实现这一点提供一些指导。
非常感谢和亲切的问候
答案 0 :(得分:3)
如果您想运行Sub来填充橙色字段,那么我的代码会为您填写数据
Option Explicit
Sub GetSolution()
' declaring a variable of Worksheet type for easier reference
' instead of saying: Sheets("All Data") you can use a shorter
' variable name: "data"
Dim data As Worksheet
Set data = Sheets("All Data")
Dim result As Worksheet
Set result = Sheets("Slide 5")
' these two variables hold the SUM for both analyst types
' in the loops flow
Dim seniorAnalystSum As Double
Dim analystSum As Double
' using resource and projectLob Range objects for quick reference to cells
' in both spreadsheets
' so again, instead of saying data.Range("A" & i) etc
' simple and short way is "resource" and/or "projectLob"
Dim resource As Range
Dim projectLob As Range
' for each cell in between K8:K16 on the result/ Sheets("Slide 5")
' pick up one cell at a time
For Each resource In result.Range("K8:K16")
' and go over all cells between B5 and last cell in Data sheet
For Each projectLob In data.Range("B5:B" & data.Range("B" & Rows.Count).End(xlUp).Row)
If projectLob = resource Then
' compare year and month between the matching cells
If (Month(projectLob.Offset(0, 8)) = Month(result.Range("B3"))) And _
(Year(projectLob.Offset(0, 8)) = Year(result.Range("B3"))) Then
' compare to the row of the table that holds the type of analyst
If projectLob.Offset(0, 7) = result.Range("L6") Then
'C&R Senior Analyst
seniorAnalystSum = seniorAnalystSum + projectLob.Offset(0, 12)
ElseIf projectLob.Offset(0, 7) = result.Range("N6") Then
'C&R Analyst
analystSum = analystSum + projectLob.Offset(0, 12)
End If
End If
End If
Next
' Assigns the sum for the senior analyst
resource.Offset(0, 2) = seniorAnalystSum
' assigns the sum for normal analyst
resource.Offset(0, 4) = analystSum
' reset both sums for the next loop
seniorAnalystSum = 0
analystSum = 0
Next
End Sub
我已使用您的样本数据并按如下方式填充结果
我希望这是您正在寻找的解决方案:)等待您的反馈
答案 1 :(得分:-1)
在SLIDE5 COLUMN M
=SUMPRODUCT(('All Data'!$I$5:$I$40='Slide 5'!$L$6:$M$6)*('All Data'!$J$5:$J$40='Slide 5'!$B$3)*('All Data'!$B$5:$B$40='Slide 5'!$K8)*('All Data'!$N$5:$N$40))
并在SLIDE5 COLUMN O
使用此
=SUMPRODUCT(('All Data'!$I$5:$I$40='Slide 5'!$L$6:$M$6)*('All Data'!$J$5:$J$40='Slide 5'!$B$3)*('All Data'!$B$5:$B$40='Slide 5'!$K8)*('All Data'!$N$5:$N$40))
将公式拖到后续行
答案 2 :(得分:-1)
快速而肮脏的解决方案
1 - 使用 评估 运算符获取预期结果
Evaluate('SUMPRODUCT(
('All Data'!$I$5:$I$40='Slide 5'!$L$6:$M$6)
*('All Data'!$J$5:$J$40='Slide 5'!$B$3)
*('All Data'!$B$5:$B$40='Slide 5'!$K8)
*('All Data'!$N$5:$N$40))'
)
2 - 使用 Application.WorksheetFunction.Sumproduct 函数替换SUMPRODUCT wokrksheet函数