Getting an Index/Match with a single criteria to sum its results - indexing

I am using an INDEX/MATCH to locate a facility number, then go to the appropriate column and return the first non-blank answer it receives. this is great. However, I can't figure out how to add a SUM function to this, so it will add all of the index match results, and not just stop at the first one it finds. I'd like to only use a formula, not VB. This is what my array currently looks like.
=INDEX(INDIRECT("$H"&(MATCH(M45,$B:$B,0))&":$H$10000"),MATCH(FALSE, ISBLANK(INDIRECT("$H"&(MATCH(M45,$B:$B,0))&":$H$10000")), 0))

Related

Excel formula not working as expected

I have a sheet that shows max values spent anywhere. So I need to find most expensive place and return it's name. Like this:
Whole sheet.
Function.
Function in text:
=IFS((A6=MAX(D2:D31)),(INDEX(C2:C31,MATCH(A6,D2:D31,0))),(A6=MAX(H2:H31)),(INDEX(G2:G31,MATCH(A6,H2:H31,0))),(A6=MAX(K2:K31)),(INDEX(K2:K31,MATCH(A6,L2:L31,0))))
Basically I need to find a word left to value, matching A6 cell.
Thanks in advance.
Ok.. Overcomplicated!
Firstly, why the three rows? it's a lot easier if you just have one long row with all the data (tell me if you actually need 3 I'll change my solution)
=LOOKUP(MAX(D2:D31);D2:D31;C2:C31)
The MAX formula will lookup the biggest value in the list, the Lookup formula will then match it to the name.
Please note: If more than one object has the maximum price, it will only return the first one. The only way I can think of to bypass that would be to build a macro.
EDIT:
Alright.. Multi Column solution is ugly and requires extra columns that you can just hide.
As you can see you'll need 2 new columns that will find the highest for each row, 2 new columns that will find the value for each of these "highest" (in this case tree and blueberries) and then your visible answer will simply be an if statement finding out which one is bigger and giving the final verdict. This can be expanded with an infinite number of columns but increases complexity.
Here are the formulas:
MAX(H2:H31)
LOOKUP(A5;H2:H31;G2:G31)
MAX(L2:L31)
LOOKUP(C5;L2:L6;K2:K6)
IF(A5>C5;B5;D5)

Count number of visible rows with criteria

In the beginning I used this formula SUBTOTAL(3;(A14:A150>0)) and it works perfectly but now I need to add a condition that I must count the number of rows where the value in the column F is greater than 0.
This formula COUNTIF(F14:F150;">0") return the right result, but when I filter I don’t have the right one.
I tried also SUMPRODUCT(SUBTOTAL(3;F14:F150)/A1*(F14:F150>0)) it return the same thing as the COUNTIF.
You can add another column with formula:
=IF(SUBTOTAL(103;F14))
Because you are using filters this formula will help you determine if your cell is hidden or not.
Now you can use, assuming that latter formula is in column G:
=COUNTIFS(F14:F150;">0";G14:G150;">0")
Array-entered using Ctrl+Shift+Enter:
=SUMPRODUCT(--(F14:F150>0),IF(SUBTOTAL(3,OFFSET(F14:F150,ROW(F14:F150)-MIN(ROW(F14:F150)),,1)),F14:F150,""))/A1
I don't get exactly your needs but you may want to use
=SUMIFS(F14:F150,F14:F150,">0",A14:A150,">0")
or
=COUNTIFS(F14:F150,">0",A14:A150,">0")

Count Unique Text Based on 2 Criteria

I was playing around with a couple ideas to count unique text based on two criteria. I was thinking Sumproduct would do it for me, but it doesn't seem to work. I may need some kind of VBA script. Basically, I want to do the following.
1) Look at everything in ColumnB and if that matches a criteria
2) Look at everything in ColumnW and if that matches a criteria
3) Count unique text in ColumnF.
It's almost like this:
=COUNTIFS(W:W,A1,B:B,B1)
THEN, based on the result of that, count uniques in ColumnF
I was thinking this should be pretty easy, but it's turning out to be really hard!
You can use a standard SUMPRODUCT-based pseudo-COUNTUNIQUE but you need to modify it by adding the criteria in the numerator and also the inverse criteria in the demononator; the latter to avoid #DIV/0! errors. This will generate a pseudo-COUNTIFSUNIQUE.
=SUMPRODUCT(SIGN((B2:B20="bee")*(W2:W20="double-you"))/
(COUNTIFS(B2:B20, "bee", W2:W20, "double-you", F2:F20, F2:F20)+(B2:B20<>"bee")+(W2:W20<>"double-you")))
Another solution is to adapt the formula from here: Count Unique Text Values in a Range.
Instead of using F:F, you modify it to match F:F if both Crit1 and Crit2 hold and "No Match" otherwise. That is,
IF((B:B="Crit1")*(W:W="Crit2"),F:F,"No Match")
Then you do a unique count on that array.
=SUMPRODUCT(--(FREQUENCY(MATCH(IF((B:B="Crit1")*(W:W="Crit2"),F:F,"No Match"),
IF((B:B="Crit1")*(W:W="Crit2"),F:F,"No Match"),0),
ROW(F:F)-ROW($F$1)+1)>0))
-NOT(PRODUCT((B:B="Crit1")*(W:W="Crit2")))
The -NOT(PRODUCT(...)) at the end is to subtract the unique count for the "No Match" entry if it exists (this can be replaced by just -1 if you know there will always be things that don't match both criteria).
Note that this is an array formula and must be entered using Ctrl+Shift+Enter.

Looking up values in a table with VBA

I am trying to lookup values in an excel table using VBA and having trouble getting it all together. I get most of the way there but my code is not worth posting.
What I need to do is write a function with several parameters:
1. What value to look up
2. What column name to look this up in
3. What other column to return the value from
For example, in a table containing Customer_ID, Last_Name, First_Name: pass it "DoeJohn", "Customer_ID", "First_Name" and have it return John. I am not concerned with duplicate values of what I am looking up, finding the first instance is good enough.
The cell it finds may be text, numeric, or date so it needs to return that.
If it doesn't find a match it should return something that could never be an actual cell value, and my main program will check for that.
If you are familiar with writing Functions() in vba you could simply have a custom function that contained a few vlookup() functions then organize the output and return it however you want.

Count unique string variants

There could be quite a simple solution to this, but I am trying to find the number of times a unique variant (i.e. non-duplicates) of a string appears in a column. However this string is only part of the text contained in a cell, and not the entire cell. To illustrate:
EuropeSpainMadrid
EuropeSpainBarcelona
AsiaChinaShanghai
AsiaJapanTokyo
EuropeEnglandLondon
EuropeSpainMadrid
I would like to find how many unique instances there are of a string that contains "EuropeSpain". So using this example, I would find that a variant of "EuropeSpain" appears only twice (given that the second instance of "EuropeSpainMadrid" is a duplicate).
A solution to this is to use pivots to summarise the data and remove duplicated; however given that my underlying dataset changes often this would require manual adjustments and corrections. I would therefore like to avoid adding any intermediate steps (i.e. PivotTables, other data sets etc) between my data and the counts.
UPDATE: I now understand to use wildcards to solve the first part of my question (counting the occurrences of "EuropeSpain"), however I am not yet clear on the second part of my question (how to find the number of unique occurrences).
Is there a formula or VBA code that could do this?
Using wildcards:
=COUNTIF(A1:A6,"="&"*"&C1&"*")
For without VBA but with some versatility, I suggest with Text in ColumnA (labelled), ColumnB labelled Flag and EuropeSpain in C1:
=FIND(C$1,A2)
in B2 copied down.
Then pivot A:B with Flag for FILTERS (and 1 selected), Text for ROWS and Count of Text for Sigma VALUES.
Apply Distinct Values if required (and available!), alternatively a formula of the kind:
=MATCH("Grand Total",E:E)-4
would count uniques.