Finding the alphabetically-first item in a table that meets certain conditions

StarliteLemming

New Member
Joined
Jun 2, 2022
Messages
12
Office Version
  1. 2019
Platform
  1. Windows
I have a table of data that I can't sort (because it's the merge of a Query with static data, and sorting messes up the references to the query -- a whole other issue!).

I want to know which record is alphabetically first out of a subset of the table, determined by certain conditions.

Currently, I'm using a rather clever solution I found online, but there is a noticeable calculation overhead because every item in the list has to be assigned a rank. So the calculation is n^2, and n is currently about 350.

Excel Formula:
=IFERROR(FIND(S$1, $P2)*0 + COUNTIFS($N:$N, $N2, $P:$P, "*" & S$1 & "*",  $B:$B, "<=" & $B2) = 1, FALSE)

To explain: the FIND() is required to generate an error if column P doesn't meet the criteria. The COUNTIFS() conditions are: 1) only rank records that have the same value in column N as the current record; 2) only rank records where the column heading, S1, appears in the text in column P; and 3) count how many records in this set are alphabetically less than the current record, in column B (the sort key). Because I only care about the first record in each set, I set TRUE if the rank is 1 (first) and FALSE otherwise.

As I said, the rank gets calculated for every record in the table, and that produces a noticeable delay from Excel. While the values in column N are all between 0 and 9, I need to actually determine the first record for those ten values for multiple (maybe a dozen) text values S1. In some cases, the results will be the same, but in others they won't. Regardless, I don't need the rank of every record to be calculated for each header value.

So, how do I do it?

If the sort were numeric, it would be easy (just use MINIFS()). Because it's an alphabetic sort (and I can't re-sort the data itself), I need to use functions that will compare alphabetic values.

My guess is that an array formula may be needed, but I've never truly understood how those work -- especially because there's at least two different types.

So, how do I determine the alphabetically-first record that meets two other criteria from other columns of the table?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
with a small macro ?
Can you give an example ? (5 lines)
 
Upvote 0
aura-of-negation4C
fireball3
fireblade3C
aura-of-deception4
aura-of-life
4C
aura-of-purity
4C
aura-of-vitality
3C
fire-aura3C

I want the value for 3 to be aura-of-vitality and the value for 4 to be aura-of-life.

Why? Because I want a separate value for every level number, and I only want to include rows which have a C in the third column. And I want to identify the alphabetically first name that meets the criteria, but I can't sort the list.

In algorithmic terms, here's what I'm thinking:
- Return a range that is a subset of the first column, including only entries whose other fields meet the criteria.
- Identify the alphabetically first item in the sublist by traversing the sublist.

The problem with using VBA is that it makes it much harder to distribute the file. I'd much rather have a clever formula. But the only formulae I've seen are super inefficient because they compare every name with the current name -- for every name. So a list of 350 items results in 350^2 = 122,500 alphabetic comparisons!
 
Upvote 0
you can upgrade to 2021-365 for appropriate formulas or use PQ (but that's not my speciality ...
 
Upvote 0
Map1
ABCDEFGHI
1aura-of-negation4C33C9E+99
2fireball37aura-of-vitality9E+99
3fireblade3C88
4aura-of-deception419E+99
5aura-of-life4C29E+99
6aura-of-purity4C49E+99
7aura-of-vitality3C55
8fire-aura3C66
99E+99
109E+99
119E+99
129E+99
139E+99
149E+99
159E+99
169E+99
179E+99
189E+99
199E+99
209E+99
Blad1
Cell Formulas
RangeFormula
H2H2=INDEX(MyA,MATCH(MIN($I$1:$I$20),I1:$I$20,0))
D1:D8D1=COUNTIF($A$1:$A$8,"<=" &A1)
I1:I20I1=IF(AND(ROW()<=ROWS(MyA),B1=$G$1,C1=$H$1),COUNTIF(MyA,"<="&A1),9E+99)
Named Ranges
NameRefers ToCells
data=Blad1!$A$1:$C$8H2, I1:I20, D1:D8
MyA=Blad1!$A$1:$A$8H2, I1:I20, D1:D8
 
Upvote 0
EDIT : i was changing previous post, but at a moment unable to modify, because of the 10 minutes limit ???
So moderator, can you delete that post ?

with a formula like column G (could find a arrayformula)
Map1
ABCDEFG
1aura-of-negation4C3C9E+99
2fireball3aura-of-vitality9E+99
3fireblade3C8
4aura-of-deception49E+99
5aura-of-life4C9E+99
6aura-of-purity4C9E+99
7aura-of-vitality3C5
8fire-aura3C6
99E+99
109E+99
119E+99
129E+99
139E+99
149E+99
159E+99
169E+99
179E+99
189E+99
199E+99
209E+99
Blad1
Cell Formulas
RangeFormula
F2F2=INDEX(MyA,MATCH(MIN($G$1:$G$20),G1:$G$20,0))
G1:G20G1=IF(AND(ROW()<=ROWS(MyA),B1=$E$1,C1=$F$1),COUNTIF(MyA,"<="&A1),9E+99)
Named Ranges
NameRefers ToCells
data=Blad1!$A$1:$C$8F2, G1:G20
MyA=Blad1!$A$1:$A$8F2, G1:G20
 
Upvote 0
Not sure i understood what you're looking for

Maybe...

Pasta1
ABCDEFG
1TextVal1Val2Crit1Crit2Result
2aura-of-negation4C3Caura-of-vitality
3fireball34Caura-of-life
4fireblade3C
5aura-of-deception4
6aura-of-life4C
7aura-of-purity4C
8aura-of-vitality3C
9fire-aura3C
Plan1
Cell Formulas
RangeFormula
G2:G3G2=INDEX(A$2:A$9,MATCH(SMALL(IF(B$2:B$9=E2,IF(C$2:C$9=F2,COUNTIF(A$2:A$9,"<"&A$2:A$9))),1),IF(B$2:B$9=E2,IF(C$2:C$9=F2,COUNTIF(A$2:A$9,"<"&A$2:A$9))),0))
Press CTRL+SHIFT+ENTER to enter array formulas.


Hope this helps

M.
 
Upvote 0
As always, Excel formulae require a bit of time to interpret. I'll have a play around with both suggestions and see what works.
Thanks muchly.

For the record, if the data were numeric rather than text, I'd be using something like:

Excel Formula:
=MINIFS(A:A, B:B, 3, C:C, "C")

The issue is entirely to do with the data being text.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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