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
LVL 6
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Older than dirtCommented:
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?
0
Author Commented:
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.
0
Older than dirtCommented:
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
``````
0
Author Commented:
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
0
Older than dirtCommented:
Change line 16 to

If UCase(Cells(lngRow, "C").Value) = UCase(Cells(lngRow, "D").Value) Then
0
Older than dirtCommented:
Found another problem. Be back soon.
0
Older than dirtCommented:
I changed a bunch of other values to test various conditions and this seems to work.

``````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 UCase(Cells(lngRow, "C").Value) = UCase(Cells(lngRow, "D").Value) Then
If lngDomRow = 0 Then
lngDomRow = lngRow
End If
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")
Next
strOrderID = Cells(lngRow, "A").Value
lngStartRow = lngRow
lngDomRow = 0 'lngRow
End If
Next
End Sub
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Martin
you are my hero

thanks very much.  it worked.
0
Older than dirtCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.