Link to home
Start Free TrialLog in
Avatar of Flora Edwards
Flora EdwardsFlag for Sweden

asked on

Excel formula needed to Return text ID that its sum has Maximum Value

Hello,

I need help with a formula solution. Please open the attached sample file.
I need to find the desired result shown in Column F based on the sample data from column A to E
first condition is, the dominant category should be run for each unique Order ID and second criteria is to exclude any amount where the values of Column D and E do not match (I highlighted them in red text) then sum order total for each category ID for each Unique Order ID and then see which of the category ID has the maximum value then whatever category ID has the max value for that specific Order ID then in Column F populate with formula the category ID which has the Maximum sum per that Order ID.

might be a easy formula, but could not get my head around it.

any help is appreciated.

Thanks a lot.
EE.xlsx
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I don't believe a formula can do what you want. I could however provide a macro so please let me know if that's OK.

A few questions up front.

1.    Is column F needed by you?
2.    You say "exclude any amount where the values of Column D and E do not match" but it looks like you mean C and D. Please confirm.
3.    Do you need the colors?
Avatar of Flora Edwards

ASKER

thanks Martin.

yes i meant column C and D.  i do not need any colors. i only used colors so that it is easy to understand the question.   Column F is the needed result, after runnign the macro the result should appear as it is portrayed in the sample in Column F.

thanks alot.
Try this.

Sub Dominant()
Dim lngLastRow As Long
Dim lngRow As Long
Dim lngStartRow As Long
Dim lngMarkRow As Long
Dim strOrderID As String
Dim lngDomRow As Long

lngLastRow = Range("A1048576").End(xlUp).Row
strOrderID = Range("A2").Value
lngDomRow = 2
lngStartRow = 2

For lngRow = 2 To lngLastRow + 1
    If Cells(lngRow, "A").Value = strOrderID Then
        If Cells(lngRow, "C").Value = Cells(lngRow, "D").Value Then
            If Cells(lngRow, "B").Value > Cells(lngDomRow, "B").Value Then
                lngDomRow = lngRow
            End If
        End If
    Else
        For lngMarkRow = lngStartRow To lngRow - 1
            Cells(lngMarkRow, "F").Value = Cells(lngDomRow, "E")
            strOrderID = Cells(lngRow, "A").Value
            lngStartRow = lngRow
        Next
    End If
Next

Open in new window

thanks Martin.

but it did not seem to solve the issue, becuase if you change the cell B6 to One million then run the macro it still bring Apple while the Banana amount is the highest and it should result banana
Change line 16 to

        If UCase(Cells(lngRow, "C").Value) = UCase(Cells(lngRow, "D").Value) Then
Found another problem. Be back soon.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Martin
you are my hero

thanks very much.  it worked.
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you
including these two new ones.
Creating your own Excel Formulas and doing the impossible
A Guide to Writing Understandable and Maintainable VBA Code
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014