Dr.PB
Dr.PB

Reputation: 1067

For loop in Each Row of Union

Function Atotal(AB As Range, values As Range) As Double
    Atotal = 0  
    Items = Union(AB.Cells, values.Cells)

    For Each hrow In Items.Rows
       ' If AB cell.Value =="A" then
       ' Atotal = Atotal + values cell.Value
    Next hrow
End Function

For Example:

AB  |   VAL
------------
A   |   5
A   |   2
B   |   5
A   |   3
B   |   1

Atotal should be 10 (=5+2+3)

How to get this? How to loop through each row of Union and get the values of different range?

Upvotes: 0

Views: 71

Answers (1)

healey
healey

Reputation: 314

There is no need to use Union in this process.

Function Atotal(AB As Range, values As Range) As Double
    Atotal = 0

    Dim i As Integer
    For i = 1 To values.Rows.Count
        If AB.Cells(i, 1) = "A" Then
            Atotal = Atotal + values.Cells(i, 1).Value
        End If
    Next i

End Function

Upvotes: 1

Related Questions