VBA Code for Maximum Value

erenkey

Board Regular
Joined
Dec 9, 2005
Messages
162
I am looking for a VBA code that will look at a spreadsheet with Name in Column A and a Value in Column B, and pull back the maximum value for each name that is in the spreadsheet. each name can be in the spreadsheet up to 3 different times but I only want the maximum value pulled back.

Can anyone help me?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You should be able to do this with a pivot table. However, if you absolutely need it to be VBA, try the following code. This is untested (and my first time using the Scripting.Dictionary), but try the following. It will create the list of unique names in column D and their max values in Column E.

Code:
Public Sub MaxValue()
Dim i       As Long, _
    LR      As Long, _
    rng     As Range, _
    rng1    As String, _
    tmp     As Double, _
    dic     As Variant, _
    rowx    As Long
    
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row
Set dic = CreateObject("Scripting.Dictionary")
rowx = 1
For i = 1 To LR - 1
    tmp = -999999
    Application.StatusBar = "Currently on row " & i & " of " & LR
    If Not dic.exists(Range("A" & i).Value) Then
        dic.Add Range("A" & i).Value
        With Range("A1:A" & LR)
            Set rng = .Find(Range("A" & i).Value, LookIn:=xlValues)
            If Not rng Is Nothing Then
                rng1 = rng.Address
                Do
                    If rng.Offset(0, 1).Value > tmp Then
                        tmp = rng.Offset(0, 1)
                    End If
                Loop While Not rng Is Nothing And rng.Address <> rng1
            End If
        End With
        Range("D" & rowx).Value = Range("A" & i).Value
        Range("E" & rowx).Value = tmp
        rowx = rowx + 1
    End If
Next i
      
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
    .StatusBar = False
End With
End Sub
 
Upvote 0
Strange, it compiles perfectly fine on my end.

Can you please provide some sample data and I will test it on my end?
 
Upvote 0
My employer will not allow me to download anything that will show my data. I simply have Name in Column A and a Numeric value in Column B.

Column A Column B
Chris 74
Greg 240
John 11
Greg 23
Chris 2161
Bob 150
Greg 195
Chris 68
Bob 221
John 25
John 259
Bob 5
Chris 13
Bob 178
John 123
Greg 122


I started over to make sure that I did it correctly. The data is in Sheet 1. I opened the VBA Editor. Right clicked on the top left window VBA Project and inserted a module. I copied your code and pasted into the module window that opened. I hit run and received a run time error '450': Wrong number of argumentsor invalid property assignment. I ran the debug and it highlighted the line

dic.Add Range("A" & i).Value
 
Upvote 0
Thank you for the sample data.

It was an error on my end, slight issue with that Add statement. I tested the following code and it worked.

Code:
Public Sub MaxValue()
Dim i       As Long, _
    LR      As Long, _
    rng     As Range, _
    rng1    As String, _
    tmp     As Double, _
    dic     As Variant, _
    rowx    As Long
    
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row
Set dic = CreateObject("Scripting.Dictionary")
rowx = 1
For i = 1 To LR - 1
    tmp = -999999
    Application.StatusBar = "Currently on row " & i & " of " & LR
    If Not dic.exists(Range("A" & i).Value) Then
        dic.Add Range("A" & i).Value[COLOR=red][B], 1
[/B][/COLOR]        With Range("A1:A" & LR)
            Set rng = .Find(Range("A" & i).Value, LookIn:=xlValues)
            If Not rng Is Nothing Then
                rng1 = rng.Address
                Do
                    If rng.Offset(0, 1).Value > tmp Then
                        tmp = rng.Offset(0, 1)
                    End If
                Loop While Not rng Is Nothing And rng.Address <> rng1
            End If
        End With
        Range("D" & rowx).Value = Range("A" & i).Value
        Range("E" & rowx).Value = tmp
        rowx = rowx + 1
    End If
Next i
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
    .StatusBar = False
End With
End Sub
 
Upvote 0
When I try to run it I get a mismatch type error. If I take out the headers and run it it works. Where can I change the code to account for the headers in row 1?

If I wanted to change my data sheet to include last name in Column B causing the numeric values to shift to Column C, how would I make that change in the code?
 
Upvote 0
Code:
Public Sub MaxValue()
Dim i       As Long, _
    LR      As Long, _
    rng     As Range, _
    rng1    As String, _
    tmp     As Double, _
    dic     As Variant, _
    rowx    As Long
    
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row
Set dic = CreateObject("Scripting.Dictionary")
rowx = 1
For i = [COLOR=red][B]2[/B][/COLOR] To LR - 1
    tmp = -999999
    Application.StatusBar = "Currently on row " & i & " of " & LR
    If Not dic.exists(Range("A" & i).Value) Then
        dic.Add Range("A" & i).Value, 1
        With Range("A1:A" & LR)
            Set rng = .Find(Range("A" & i).Value, LookIn:=xlValues)
            If Not rng Is Nothing Then
                rng1 = rng.Address
                Do
                    If rng.Offset(0, 1).Value > tmp Then
                        tmp = rng.Offset(0, 1)
                    End If
                Loop While Not rng Is Nothing And rng.Address <> rng1
            End If
        End With
        Range("D" & rowx).Value = Range("A" & i).Value
        Range("E" & rowx).Value = tmp
        rowx = rowx + 1
    End If
Next i
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
    .StatusBar = False
End With
End Sub
 
Upvote 0
Can you sort the results descending by value in the code?

Sure can, try:

Code:
Public Sub MaxValue()
Dim i       As Long, _
    LR      As Long, _
    rng     As Range, _
    rng1    As String, _
    tmp     As Double, _
    dic     As Variant, _
    rowx    As Long
    
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
LR = Range("A" & Rows.Count).End(xlUp).Row
Set dic = CreateObject("Scripting.Dictionary")
rowx = 1
For i = 2 To LR - 1
    tmp = -999999
    Application.StatusBar = "Currently on row " & i & " of " & LR
    If Not dic.exists(Range("A" & i).Value) Then
        dic.Add Range("A" & i).Value, 1
        With Range("A1:A" & LR)
            Set rng = .Find(Range("A" & i).Value, LookIn:=xlValues)
            If Not rng Is Nothing Then
                rng1 = rng.Address
                Do
                    If rng.Offset(0, 1).Value > tmp Then
                        tmp = rng.Offset(0, 1)
                    End If
                Loop While Not rng Is Nothing And rng.Address <> rng1
            End If
        End With
        Range("D" & rowx).Value = Range("A" & i).Value
        Range("E" & rowx).Value = tmp
        rowx = rowx + 1
    End If
Next i
Columns("D:E").Sort Key1:=Range("E1"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
        
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationAutomatic
    .StatusBar = False
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

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