need to return maximum value among multiple records with same key using VBA

sushaanth

New Member
Joined
Mar 1, 2013
Messages
4
I am writing a VBA function that will search for a value in a column(say column B) in a table. This column can have duplicates. So my search needs to get all the records that match the value (including duplicates) and from these records i need to return only one value among records fetched that has maximum in another column in the same sheet(say column D).
Eg :

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">sno dept name marks \br1 cs sush 55 \br2 ece ram 68 \br3 cs harish 77 \br</code></pre>
So if i give "cs" value to search it must give me 77 as output. I tried to write my own Lookup function but isn't working and getting #value.
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi and welcome to Mr Excel Forum

Try this array formula

=MAX(IF($B$2:$B$100=F2,$D$2:$D$100))

confirmed with Ctrl+Shift+Enter simultaneously
(hold down both Ctrl and Shift keys and hit Enter)

Where F2 houses the value of interest (cs in your example)

M.
 
Upvote 0
Hi and welcome to Mr Excel Forum

Try this array formula

=MAX(IF($B$2:$B$100=F2,$D$2:$D$100))

confirmed with Ctrl+Shift+Enter simultaneously
(hold down both Ctrl and Shift keys and hit Enter)

Where F2 houses the value of interest (cs in your example)

M.

Thanks Marcelo,

Will this work in VBA ???
 
Upvote 0
Thanks Marcelo,

Will this work in VBA ???

You are welcome.

You dont need VBA, but if you want maybe something like this

Code:
Sub aTest()
    Dim myVar
    
    'Adjust the sheet name in red
    myVar = Evaluate("MAX(IF([COLOR=#FF0000]Sheet1[/COLOR]!$B$2:$B$100=[COLOR=#FF0000]Sheet1[/COLOR]!$F$2,[COLOR=#FF0000]Sheet1[/COLOR]!$D$2:$D$100))")
    
    MsgBox myVar
    
End Sub

M.
 
Upvote 0
I am writing a VBA function that will search for a value in a column(say column B) in a table. This column can have duplicates. So my search needs to get all the records that match the value (including duplicates) and from these records i need to return only one value among records fetched that has maximum in another column in the same sheet(say column D).
Eg :

<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">sno dept name marks \br1 cs sush 55 \br2 ece ram 68 \br3 cs harish 77 \br</code>
So if i give "cs" value to search it must give me 77 as output. I tried to write my own Lookup function but isn't working and getting #value.

Is there any way to compute the formulae without pressing ctrl+shift+enter. I have lot of records and doing this for each record is tiresome.
 
Upvote 0
Hi and welcome to Mr Excel Forum

Try this array formula

=MAX(IF($B$2:$B$100=F2,$D$2:$D$100))

confirmed with Ctrl+Shift+Enter simultaneously
(hold down both Ctrl and Shift keys and hit Enter)

Where F2 houses the value of interest (cs in your example)

M.
Is there any way to compute the formulae without pressing ctrl+shift+enter. I have lot of records and doing this for each record is tiresome.
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,549
Members
449,735
Latest member
Gary_M

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