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?
 
When I run this it does not pull the maximum for each individual name but the results are sorted descending by value.

The results that the code is giving me is:

Greg 240
Bob 150
Chris 74
John 11

It should be giving me:

Chris 2161
John 259
Greg 240
Bob 221
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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("A[COLOR=red][B]2[/B][/COLOR]: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
Found the issue - I had accidentally omitted a line in the Find loop:

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("A2: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
                    Set rng = .FindNext(rng)
                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

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style=";">A</td><td style=";">B</td><td style="text-align: right;;"></td><td style=";">Chris</td><td style="text-align: right;;">2161</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Chris</td><td style="text-align: right;;">74</td><td style="text-align: right;;"></td><td style=";">John</td><td style="text-align: right;;">259</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">Greg</td><td style="text-align: right;;">240</td><td style="text-align: right;;"></td><td style=";">Greg</td><td style="text-align: right;;">240</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">John</td><td style="text-align: right;;">11</td><td style="text-align: right;;"></td><td style=";">Bob</td><td style="text-align: right;;">221</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style=";">Greg</td><td style="text-align: right;;">23</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style=";">Chris</td><td style="text-align: right;;">2161</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style=";">Bob</td><td style="text-align: right;;">150</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style=";">Greg</td><td style="text-align: right;;">195</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style=";">Chris</td><td style="text-align: right;;">68</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style=";">Bob</td><td style="text-align: right;;">221</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style=";">John</td><td style="text-align: right;;">25</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">12</td><td style=";">John</td><td style="text-align: right;;">259</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">13</td><td style=";">Bob</td><td style="text-align: right;;">5</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">14</td><td style=";">Chris</td><td style="text-align: right;;">13</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">15</td><td style=";">Bob</td><td style="text-align: right;;">178</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">16</td><td style=";">John</td><td style="text-align: right;;">123</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">17</td><td style=";">Greg</td><td style="text-align: right;;">122</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style="background-color: #E0E0F0;text-align: center;color: #161120"><th><b>Sheet1</b></th></tr></td></thead></table><br /><br />
 
Upvote 0
That worked. Thank you for your help on this. I have one more question. I want to add last name into column B and shift the values to column E. Where would I change the code so that when it is ran it pulls back all 3 columns?
 
Upvote 0
The best place would be on Sheet2 columns A:C but if it is easier then we can put it in columns E:G on Sheet 1
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,519
Members
452,921
Latest member
BBQKING

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