在 VBA 中使用多个 SUMIFS
Using Multiple SUMIFS in VBA
如何将这个求和公式更改为 VBA 宏。
=SUMIFS('Staff Allocation'!N3:N6,'Staff Allocation'!B3:B6,Sheet2!A1,'Staff Allocation'!E3:E6,Sheet2!B1,'Staff Allocation'!F3:F6,Sheet2!C1)
+SUMIFS(Modules!H2:H4,Modules!B2:B4,Sheet2!A1,Modules!G2:G4,Sheet2!B1,Modules!E2:E4,Sheet2!C1)
所以它可以用在 if 公式中 not =0 then do a specified action else move to next process.
A1 是第一个条件值,
B1是第二个标准值,
C1为第三个条件值
您使用 Application.WorksheetFunction 访问工作表函数。
根据 the documentation for SumIfs,您需要将参数作为 Range
对象提供。
因此您需要获取每个要评估的范围并将其声明为范围对象。在Excel,Range object is a member of the Worksheet object so you need to specify which worksheet the range is on. You can do this by specifying the name or index number from the Sheets Collection。
例如。 Sheet2!A1
可以用SomeRange = Sheets("Sheet2").Range("A1")
得到
现在,Worksheet 对象是 Workbook Object 的成员,因此您还可以指定所需的工作簿 - 但如果您通读上面的文档链接,您会看到默认的Sheets 集合是 ActiveWorkbook - 所以我们将依赖它来保持示例简单。
因此,要传递所有 SumIfs 参数,您可以执行如下操作。
虽然这可能不是最优雅的解决方案,但我认为它应该很容易理解它是如何工作的。
Sub Foo()
Dim Arg1 as Range
Dim Arg2 as Range
Dim Arg3 as Range
Dim Arg4 as Range
Dim Arg5 as Range
Dim Arg6 as Range
Dim Arg7 as Range
Dim Arg8 as Range
Dim Arg9 as Range
Dim Arg10 as Range
Dim Arg11 as Range
Dim Arg12 as Range
Dim Arg13 as Range
Dim Arg14 as Range
Set Arg1=Sheets("Staff Allocation").Range("N3:N6")
Set Arg2=Sheets("Staff Allocation").Range("B3:B6")
Set Arg3=Sheets("Sheet2").Range("A1")
Set Arg4=Sheets("Staff Allocation").Range("E3:E6")
Set Arg5=Sheets("Sheet2").Range("B1")
Set Arg6=Sheets("Staff Allocation").Range("F3:F6")
Set Arg7=Sheets("Sheet2").Range("C1")
Set Arg8=Sheets("Modules").Range("H2:H4")
Set Arg9=Sheets("Modules").Range("B2:B4")
Set Arg10=Sheets("Sheet2").Range("A1")
Set Arg11=Sheets("Modules").Range("G2:G4")
Set Arg12=Sheets("Sheet2").Range("B1")
Set Arg13=Sheets("Modules").Range("E2:E4")
Set Arg14=Sheets("Sheet2").Range("C1")
If Application.WorksheetFunction.SumIfs(Arg1,Arg2,Arg3,Arg5,Arg6,Arg6,Arg7)
+ Application.WorksheetFunction.SumIfs(Arg8,Arg9,Arg10,Arg11,Arg12,Arg13,Arg14) > 0 Then
'Do Something
Else
'Do Something Else
End IF
End Sub
如何将这个求和公式更改为 VBA 宏。
=SUMIFS('Staff Allocation'!N3:N6,'Staff Allocation'!B3:B6,Sheet2!A1,'Staff Allocation'!E3:E6,Sheet2!B1,'Staff Allocation'!F3:F6,Sheet2!C1)
+SUMIFS(Modules!H2:H4,Modules!B2:B4,Sheet2!A1,Modules!G2:G4,Sheet2!B1,Modules!E2:E4,Sheet2!C1)
所以它可以用在 if 公式中 not =0 then do a specified action else move to next process.
A1 是第一个条件值, B1是第二个标准值, C1为第三个条件值
您使用 Application.WorksheetFunction 访问工作表函数。
根据 the documentation for SumIfs,您需要将参数作为 Range
对象提供。
因此您需要获取每个要评估的范围并将其声明为范围对象。在Excel,Range object is a member of the Worksheet object so you need to specify which worksheet the range is on. You can do this by specifying the name or index number from the Sheets Collection。
例如。 Sheet2!A1
可以用SomeRange = Sheets("Sheet2").Range("A1")
现在,Worksheet 对象是 Workbook Object 的成员,因此您还可以指定所需的工作簿 - 但如果您通读上面的文档链接,您会看到默认的Sheets 集合是 ActiveWorkbook - 所以我们将依赖它来保持示例简单。
因此,要传递所有 SumIfs 参数,您可以执行如下操作。 虽然这可能不是最优雅的解决方案,但我认为它应该很容易理解它是如何工作的。
Sub Foo()
Dim Arg1 as Range
Dim Arg2 as Range
Dim Arg3 as Range
Dim Arg4 as Range
Dim Arg5 as Range
Dim Arg6 as Range
Dim Arg7 as Range
Dim Arg8 as Range
Dim Arg9 as Range
Dim Arg10 as Range
Dim Arg11 as Range
Dim Arg12 as Range
Dim Arg13 as Range
Dim Arg14 as Range
Set Arg1=Sheets("Staff Allocation").Range("N3:N6")
Set Arg2=Sheets("Staff Allocation").Range("B3:B6")
Set Arg3=Sheets("Sheet2").Range("A1")
Set Arg4=Sheets("Staff Allocation").Range("E3:E6")
Set Arg5=Sheets("Sheet2").Range("B1")
Set Arg6=Sheets("Staff Allocation").Range("F3:F6")
Set Arg7=Sheets("Sheet2").Range("C1")
Set Arg8=Sheets("Modules").Range("H2:H4")
Set Arg9=Sheets("Modules").Range("B2:B4")
Set Arg10=Sheets("Sheet2").Range("A1")
Set Arg11=Sheets("Modules").Range("G2:G4")
Set Arg12=Sheets("Sheet2").Range("B1")
Set Arg13=Sheets("Modules").Range("E2:E4")
Set Arg14=Sheets("Sheet2").Range("C1")
If Application.WorksheetFunction.SumIfs(Arg1,Arg2,Arg3,Arg5,Arg6,Arg6,Arg7)
+ Application.WorksheetFunction.SumIfs(Arg8,Arg9,Arg10,Arg11,Arg12,Arg13,Arg14) > 0 Then
'Do Something
Else
'Do Something Else
End IF
End Sub