[英]Sumifs for array and range vba excel
假設我在 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, 2018.25, 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.