我想知道是否有人可以帮助我。
大约一个星期以来,我一直在尝试整理一张包含基于多个标准的“SumIf”公式的表格。
我已经能够做到这一点,在单元格范围内手动输入具有所需结果的公式。
我遇到的问题实际上是两个方面。
为了更好地说明这一点,我附上了一个文件 here显示了我想要实现的目标。
橙色阴影列,
“幻灯片 5”表, 和 ,
“幻灯片 5”表, 和 ,
K栏在 “幻灯片 5”床单。
正如我所说,我已经为此工作了一段时间,但我无法提出解决方案。
我只是想知道是否有人可以看看这个,并就我如何能够实现这一点提供一些指导。
非常感谢和亲切的问候
最佳答案
如果您想运行 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
我使用了您的示例数据并按如下方式填充了结果
我希望这是您正在寻找的解决方案 :) 等待您的反馈
关于VBA求和如果有多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301709/