如何根据条件求和VBA值?

时间:2016-02-08 10:07:50

标签: vba sum conditional-statements

我有以下设置:

C1  C2  
10  EUR 
20  GBP
40  EUR
60  USD

如何迭代这些单元格并总结所有欧元和所有其他货币?

1 个答案:

答案 0 :(得分:0)

我假设在A栏中你有金额和B栏是货币;并从第1行开始:

Sub test()

Dim ws As Worksheet
Dim SumEUR As Long, SumOtherCurrency As Long

Set ws = ThisWorkbook.Sheets("Sheet1")

With ws

For i = 1 To .Range("A" & Rows.Count).End(xlUp).Row

    If .Cells(i, "B") = "EUR" Then
        SumEUR = SumEUR + .Cells(i, "A")
    Else
        SumOtherCurrency = SumOtherCurrency + .Cells(i, "A")
    End If

Next i

End With

MsgBox ("SUM EUR :" & SumEUR & vbNewLine & "Sum Other :" & SumOtherCurrency)

End Sub