Home Ask Login Register

Developers Planet

Your answer is one click away!

user969113 February 2016

How to sum VBA values based on conditions?

I have the following setup:

C1  C2  
10  EUR 
20  GBP
40  EUR
60  USD

How can I iterate over these cells and sum up all EUR and all other currencies?


manu February 2016

I assume that in column A you have the amount and column B the currency; and start from row 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")
        SumOtherCurrency = SumOtherCurrency + .Cells(i, "A")
    End If

Next i

End With

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

End Sub

Post Status

Asked in February 2016
Viewed 2,015 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life