如何使用OR运算符与SumIfs()

时间:2017-12-26 19:08:09

标签: excel vba excel-vba

如果3个标准中的1个(= OR)匹配,我如何使用SumIfs求和?

如何使用OR作为标准?这是伪代码。

Sub SumDemo()

    Dim Sum

    Sum = Application.WorksheetFunction.SumIfs([REVENUE], [UNIT], "=avengers", [region], "=north OR =south OR = west")

    MsgBox Sum

End Sub

2 个答案:

答案 0 :(得分:3)

尝试谷歌搜索“VBA excel sumifs Or”,你会发现结果#1:

VBA - SumIfs with Or

Dim Sum as Double
Dim Revenue As Range, Unit As Range, Region As Range

'You will need to define your ranges.
Set Revenue = Range("")
Set Unit ...
Set Region ...

Sum = Application.Sum(Application.SumIfs(Revenue, Regions, Unit, "Avengers", Array("North", "south","west")))

答案 1 :(得分:1)

与重复方法略有不同,使用Evaluate来评估工作表公式。

Dim Sm As Long

Sm = ActiveSheet.Evaluate("Sum(SumIfs(REVENUE, UNIT, ""avengers"", region, {""north"", ""south"", ""west""}))")

MsgBox Sm