Function for Finding Max in an a range and outputting the name of cell VBA

bmuel13

New Member
Joined
Dec 9, 2018
Messages
2
Hello, I am stuck on creating code to find the max in a set of data and outputting the name of the cell. For Example if I have the following data set:
VIVIX0.142966875
HLIEX0.104074291
USNQX0.144564163
HNASX0.072884645
SCVIX0.068055342
MMYLX0.056515137
JDSCX0.080380966
WAMCX0.03907045

<colgroup><col><col></colgroup><tbody>
</tbody>

The funtion needs evaluate the entire data set and output the name next to the max so in this case USNQX

Thank you for your help. :)
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Re: Function for Finding Max in an a range and outputting the name of cell VBA PLEASE HELP

Here is one way.

Code:
Sub t()
Dim fn As Range
Set fn = Range("B:B").Find(Application.Max(Range("B:B")), , xlValues)
        If Not fn Is Nothing Then
            MsgBox fn.Offset(, -1).Value
        End If
End Sub

and as a function (UDF)

Code:
Function getNameInCell(rng As Range) As String
Dim fn As Range
Set fn = rng.Find(Application.Max(rng), , xlValues)
        If Not fn Is Nothing Then
            getNameInCell = fn.Offset(, -1).Value
        End If
End Function
 
Last edited:
Upvote 0
Re: Function for Finding Max in an a range and outputting the name of cell VBA PLEASE HELP

The function needs work. I am on it now.

don't know what happened, i go a bad result but now it works.

Code:
Function getNameInCell(rng As Range) As String
Dim fn As Range
Set fn = rng.Find(Application.Max(rng), , xlValues)
        If Not fn Is Nothing Then
            getNameInCell = fn.Offset(, -1).Value
        End If
End Function
 
Last edited:
Upvote 0
Re: Function for Finding Max in an a range and outputting the name of cell VBA PLEASE HELP

hey JLG , I appreciate your help. I'm still having some trouble with the function. when i try to run it, it returns a blank. Not an error but simply dissapears when I press enter. Thanks!
 
Upvote 0
Re: Function for Finding Max in an a range and outputting the name of cell VBA PLEASE HELP

hey JLG , I appreciate your help. I'm still having some trouble with the function. when i try to run it, it returns a blank. Not an error but simply dissapears when I press enter. Thanks!
The function should be put into the standard code module1 and then used like a worksheet function.

Code:
=getNameInCell(B:B)

It only returns the cell value for the cell to the immediate left of the found MAX value. The range entered in parentheses should be the range containing the values to check for Max value. You can shorten the range by inclusing the row numbers, eg. (B2:B20)

It works for me using your data in Post #1 .
 
Last edited:
Upvote 0
Re: Function for Finding Max in an a range and outputting the name of cell VBA PLEASE HELP

hey JLG , I appreciate your help. I'm still having some trouble with the function. when i try to run it, it returns a blank. Not an error but simply dissapears when I press enter. Thanks!

I also thought of the possibility that the data might not be in adjacent columns. If that is the case, then the function fails. The columns must be contguous and the search column must be right of the target column.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

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