问题描述
假设我在Excel中拥有数据,例如:
Amount Quarter Delay
378.07 2018.25 6.00
378.07 2018.25 6.00
844.36 2018.25 3.00
1134.21 2018.25 3.00
1134.21 2018.25 5.00
1512.28 2018.25 4.00
99.42 2018.75 2.00
447.38 2017.25 2.00
646.22 2018.75 2.00
745.64 2018.75 2.00
745.64 2018.75 3.00
572.4 2016.25 8.00
572.4 2016.25 8.00
572.4 2016.25 8.00
金额,季度和延迟分别在C,D,E列中。
Sub abc()
MsgBox Application.SumIfs(Range("C:C"),Range("D:D"),2018.25,Range("E:E"),3)
End Sub
我的想法是我要求和,其中Quarter = 2018.25,Delay = 3。
但是,如果我将Delay和Quarter的数据保存在一个数组中,请说“ d_delay”和“ d_quarter”,以下代码将不起作用:
Sub abc()
'assume that we have data in d_delay and d_quarter by redim and they have the length of 14
Set d_amount = Range("C2:C15")
MsgBox Application.SumIfs(d_amount,d_quarter,d_delay,3)
End Sub
你能给我建议吗?
解决方法
如果要使用数组,请将数量也设为数组,然后迭代它们并执行自己的条件。满足条件的总和:
Sub abc()
Dim d_amount As Variant
d_amount = ActiveSheet.Range("C2:C15")
Dim d_quarter As Variant
d_quarter = ActiveSheet.Range("D2:D15")
Dim d_delay As Variant
d_delay = ActiveSheet.Range("E2:E15")
Dim ttl As Double
ttl = 0#
Dim i As Long
For i = 1 To UBound(d_amount,1)
If d_quarter(i,1) = 2018.25 And d_delay(i,1) = 3 Then
ttl = ttl + d_amount(i,1)
End If
Next i
MsgBox ttl
End Sub