Function to find the maximum in range considering the artificial hierarchy placed on prefixed letter values

TonyChestnut

New Member
Joined
Aug 24, 2012
Messages
12
Hi,

Hopefully the title of this thread makes some sense. Let me explain what I am trying to do...

I have a range of values, A1:A5. In this hypothetical situation say that they are the following:

A1 = P1
A2 = T1
A3 = C4
A4 = PC1
A5 = C6

The letter/s on the left relate to a hierarchy where P is the lowest, T is next, then C, and PC is the 'highest'.

What I need is a function (cannot be a macro, and not possible to use helper cells due to the extent of the project) that will return the 'highest' result, first considering the Letter/s on the left, and then the numbers on the right.

The function should return, in the given scenario, the value of A4. If that wasn't there then the value of A5.

I've been ripping my hair out trying various combinations of MAX, IF, SUBSTITUTE, LARGE, VALUE, N, LOOKUP, etc, but I fail everytime.

Please help me win.

Kind regards,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi and welcome to Mr Excel Forum,

Question:
Is there only one digit after the letters on the left?
Or Cxx, Txx, PCxxx, Pxxx etc are also possible?

M.
 
Upvote 0
Try this array formula, confirmed with Ctrl+Shift+Enter not just Enter
Code:
=IFERROR(INDEX($A$1:$A$5,MATCH(LARGE(INDEX(INDEX(IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="P",100,IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="T",200,IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="C",300,IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="PC",400,0)))),,1)+INDEX(RIGHT($A$1:$A$5,1)*1,,),,1),ROWS($1:1)),INDEX(INDEX(IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="P",100,IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="T",200,IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="C",300,IF(INDEX(LEFT($A$1:$A$5,LEN($A$1:$A$5)-1),,1)="PC",400,0)))),,1)+INDEX(RIGHT($A$1:$A$5,1)*1,,),,1),0)),"")

This seems to work for 2010 but is to large for 2003.

If it works for you it might be better using named ranges to simplify the formula.
 
Upvote 0
Hmm?
Create the following Names
1/. DataList (Dynamic. so your range can grow as required, no blanks, Subtract one from COUNTA if you have a header)
Code:
=Sheet1!$A$1:INDEX(Sheet1!$A:$A,COUNTA(Sheet1!$A:$A))
2/. IntegerList
Code:
=INDEX(RIGHT(DataList,1)*1,,)
3/. PrefixList
Code:
=INDEX(LEFT(DataList,LEN(DataList)-1),,1)
4/. Prefix_PC
Code:
=INDEX(IF(PrefixList="PC",400,0),,1)
5/. Prefix_C
Code:
=INDEX(IF(PrefixList="C",300,0),,1)
6/. Prefix_T
Code:
=INDEX(IF(PrefixList="T",200,0),,1)
7/. Prefix_P
Code:
=INDEX(IF(PrefixList="P",100,0),,1)

The formula now becomes ...
Code:
=INDEX(DataList,MATCH(LARGE(INDEX(Prefix_P+Prefix_T+Prefix_C+Prefix_PC+IntegerList,,1),ROWS($1:1)),INDEX(Prefix_P+Prefix_T+Prefix_C+Prefix_PC+IntegerList,,1),0))

Because all names are treated as arrays this can be confirmed with Enter.

You could of course combine more names to get an even simpler end formula.

This works in 2003 so it should be okay for 2007 and above
 
Last edited:
Upvote 0
Oops! Just noticed the integer part might be wrong!
Change it to this if it is
IntegerList
Code:
=INDEX(1/RIGHT(DataList,1),,)

The formula to get the max is
Code:
=INDEX(DataList,MATCH(MAX(INDEX(Prefix_P+Prefix_T+Prefix_C+Prefix_PC+IntegerList,,1)),INDEX(Prefix_P+Prefix_T+Prefix_C+Prefix_PC+IntegerList,,1),0))
 
Last edited:
Upvote 0
This might be another "Short Cut" Name
PrefixToValue
Refers to
Code:
=INDEX(LOOKUP(LEFT(DataList,LEN(DataList)-1),{"C","P","PC","T"},{300,100,400,200}),,1)
This replaces all the prefix Names

To get a sorted list, Drag/Fill Down
Code:
=INDEX(DataList,MATCH(LARGE(INDEX(PrefixToValue+IntegerList,,1),ROWS($1:1)),INDEX(PrefixToValue+IntegerList,,1),0))
To get the max
Code:
=INDEX(DataList,MATCH(MAX(INDEX(PrefixToValue+IntegerList,,1)),INDEX(PrefixToValue+IntegerList,,1),0))

Enough for now, I'll wait for some feedback, off to the pub ...(y)
 
Upvote 0
Hi Marcol,

I tried the first formula you submitted, but it wouldn't work if there was a gap in the list. Unfortunately I need to be able to leave spaces in the list. Also it needs to work with multiple numbers on the right, for example C14, PC23, T112 etc.

Other than that it functions perfectly!

I'll look into your other replies now.

Thank you so much for your time.
 
Upvote 0

Forum statistics

Threads
1,215,300
Messages
6,124,138
Members
449,144
Latest member
Rayudo125

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