Finding the largest value

Excel_77

Active Member
Joined
Sep 15, 2016
Messages
311
Office Version
  1. 2019
Platform
  1. Windows
Hi All,

What code can I use to return the ID (column A) that has the highest associated total from column B? Note, that some IDs are duplicated so the overall totals are needed for each.

Thanks
1719433457231.png

What
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I would create a Pivot table and then select a cell in the "Total" column -> Right-click -> Sort -> Largest to smallest.
1719434664438.png
 
Upvote 0
In 2019, the formula option isn't pretty, but VBA is another option.
VBA Code:
Sub SumValuesByID()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim dict As Object
    Dim data As Variant
    Dim maxID As String
    Dim maxValue As Double

    Set dict = CreateObject("Scripting.Dictionary")
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Change sheet name to suit
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    data = ws.Range("A2:B" & lastRow).Value 'data starts in row 2.
   
   'Loop and sum
    For i = 1 To UBound(data, 1) 
        If dict.exists(data(i, 1)) Then
            dict(data(i, 1)) = dict(data(i, 1)) + data(i, 2)
        Else
            dict.Add data(i, 1), data(i, 2)
        End If
    Next i
   
    'Find the max ID and amount
    maxValue = 0
    For Each ID In dict.Keys
        If dict(ID) > maxValue Then
            maxValue = dict(ID)
            maxID = ID
        End If
    Next ID
   
   'Output
    ws.Range("C1").Value = maxID
    ws.Range("D1").Value = maxValue

    Set dict = Nothing
End Sub
 
Upvote 0
In 2019, the formula option isn't pretty, but VBA is another option.
VBA Code:
Sub SumValuesByID()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
    Dim dict As Object
    Dim data As Variant
    Dim maxID As String
    Dim maxValue As Double

    Set dict = CreateObject("Scripting.Dictionary")
    Set ws = ThisWorkbook.Sheets("Sheet1") 'Change sheet name to suit
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    data = ws.Range("A2:B" & lastRow).Value 'data starts in row 2.
  
   'Loop and sum
    For i = 1 To UBound(data, 1)
        If dict.exists(data(i, 1)) Then
            dict(data(i, 1)) = dict(data(i, 1)) + data(i, 2)
        Else
            dict.Add data(i, 1), data(i, 2)
        End If
    Next i
  
    'Find the max ID and amount
    maxValue = 0
    For Each ID In dict.Keys
        If dict(ID) > maxValue Then
            maxValue = dict(ID)
            maxID = ID
        End If
    Next ID
  
   'Output
    ws.Range("C1").Value = maxID
    ws.Range("D1").Value = maxValue

    Set dict = Nothing
End Sub
I don't use VBA.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top