[英]Sumifs for array and range vba excel
Let say I have the data in Excel like:假设我在 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
The Amount, Quarter and Delay are in column C, D, E respectively.金额、季度和延迟分别在 C、D、E 列中。
And I have the following code:我有以下代码:
Sub abc()
MsgBox Application.SumIfs(Range("C:C"), Range("D:D"), 2018.25, Range("E:E"), 3)
End Sub
The idea is that I want to take the sum where Quarter = 2018.25 and Delay = 3.这个想法是我想取 Quarter = 2018.25 和 Delay = 3 的总和。
However, in the case that I save the data of Delay and Quarter in an array, let say "d_delay" and "d_quarter", the following code does not work:但是,如果我将 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, 2018.25, d_delay, 3)
End Sub
Can you give me advice on this?你能给我建议吗?
If you are going to use arrays, make the amount an array also and iterate them and do your own conditionals.如果您要使用数组,请将数量也设为数组并迭代它们并执行您自己的条件。 Sum when the conditions are met:
满足条件时求和:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.