Find largest number in list, repeat process

Raptor776

New Member
Joined
Aug 29, 2014
Messages
46
Hello,

I wanted to ask how I could write a macro that would go through a list and find the largest number correlating with the values in A. For example

Name
Object
Apple
5
Apple
3
Apple
90
Orange
1
Orange
5
Orange
9
Orange
8
Orange
2
Mango
89
Mango
45

<tbody>
</tbody>

Therefore in Column C, it should display the largest value correlating with each set in A. So 90 in C4, 9 in C7, and 89 in C10.

The issue that I am having is that the number of rows over which the maximum formula applies is not constant. I was hoping to get some help with that. Thank you!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

NeonRedSharpie

Well-known Member
Joined
Jul 14, 2014
Messages
1,678
Code:
Function MAXIF(rng As Range, nCell As Range, val As Range) As Variant

    Dim arr As Variant
    arr = val


    For Each cell In rng
        If cell.Value = nCell.Value Then
            If val(cell.Row) > MAXIF Then MAXIF = val(cell.Row)
        End If
    Next cell
End Function

I don't know if you want a UDF, but if you do here is one. The input would look like:

Code:
=maxif($A$1:$A$10,$A1,$B$1:$B$10)

You can also call this code in an existing macro.
 
Upvote 0

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,849
Office Version
  1. 365
Platform
  1. Windows
Probably few different ways but this in C2 and copied down:

=IF(B2=MAX((IF($A$2:$A$1000=A2,1,0)*$B$2:$B$1000)),MAX((IF($A$2:$A$1000=A2,1,0)*$B$2:$B$1000)),"")

CNTL-SHIFT-ENTER to enter
 
Upvote 0

MickG

MrExcel MVP
Joined
Jan 9, 2008
Messages
14,841
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG11Sep24
[COLOR="Navy"]Dim[/COLOR] Rng         [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn          [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
    .CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Value) [COLOR="Navy"]Then[/COLOR]
        .Add Dn.Value, Dn.Offset(, 1)
    [COLOR="Navy"]Else[/COLOR]
        [COLOR="Navy"]Set[/COLOR] .Item(Dn.Value) = Union(.Item(Dn.Value), Dn.Offset(, 1))
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]Dim[/COLOR] k
[COLOR="Navy"]Dim[/COLOR] G
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] k [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] G [COLOR="Navy"]In[/COLOR] .Item(k)
        [COLOR="Navy"]If[/COLOR] G = Application.Max(.Item(k)) [COLOR="Navy"]Then[/COLOR]
            G.Offset(, 1) = Application.Max(.Item(k))
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] G
[COLOR="Navy"]Next[/COLOR] k
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,200
Try In A2

=IF(SUMPRODUCT(MAX(--($A$2:$A$5000=A2)*$B$2:$B$5000))=B2,B2,"")
 
Upvote 0

Forum statistics

Threads
1,191,234
Messages
5,985,435
Members
439,965
Latest member
mcsmith1974

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
Top