troubleshooting Question

VBA Function for a Sumif

Avatar of matt_m
matt_m asked on
Microsoft Excel
5 Comments1 Solution631 ViewsLast Modified:
Hi All,

I have been asked at work to produce a formula which will do a sumif on a range in a single row where the criteria are in adjacent cells.

For example 20,1, 10,1,15,2

The result would sumif if the cell to the right is 1 - giving an answer of 30 in the above array.

I am struggling to produce a formula that can do this so I have gone to a custom function.  What I have  is below (which doesn't work)


Public Function MattsSumIF(Criteria As Double, NRange as range)

Dim Cell As Range
Total = 0
i = 1
For Each Cell In NRange
    If ActiveCell.Offset(0, i).Value = Criteria Then
    Total = ActiveCell.Value + Total
    End If
   
        i = i + 1
       
Next Cell
   
    MattsSumIF = Total
   
   
End Function

Any ideas?

Thanks
Matt
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros