Excel - How to sum concatenated substrings - excel

I've got a problem I'm trying to work out in excel, and I've only been able to find the solution to the opposite problem.
I've got a list of data in a form similar to the following:
A | 10
B | 15
C | 12
D | 17
And I want to be able to make a string of any of those strings once and get the sum. The results table would look something like this:
A | 10
A, B | 25
A, C | 22
A, C, D | 39
In all I've been able to find, I haven't been able to find a way to check if a string contains other strings as substrings, then adds an associated value to a total if that is true.

One approach, with SUMPRODUCT and SEARCH.
=SUMPRODUCT(ISNUMBER(SEARCH($A$1:$A$4,E1))*$B$1:$B$4)
Do be aware of pitfalls though when matching substrings; for example, AA in E1 would still return 10 in F1.

In addition to #BigBen, you could try in cell F1:
=SUMPRODUCT(SUMIF(A$1:A$4,FILTERXML("<t><s>"&SUBSTITUTE(E1,", ","</s><s>")&"</s></t>","//s"),B$1:B$4))
That should get rid of the possible false positives.

Related

sum values of cells in google sheets if the result of their sum with adjacent cell meets a condition

Suppose I have the following data in a spreadsheet, where the first row contains the column headings and the first column contains the row index (for reference):
||A | B | C |
==||================
0 ||2 | 3 | Y |
--||----------------
1 ||2 | 4 | Y |
--||----------------
2 ||3 | 5 | N |
--||----------------
3 ||8 | 3 | Y |
--||----------------
How can I get the sum of all the values in column B for which b - a >= 1 && c == "Y", in Google Sheets and Excel?
So essentially, the sum should factor in only rows 0 and 1 in which case the result should be 7.
I know this sounds like a very specific question but I did not know how else to describe it other than by example. The answer should be applicable in other similar scenarios.
Thanks for the help.
[Edit] In response to people voting down due to lack of research, well, I've tried to use the sumif() function but I got stuck immediately on the condition part as I am not sure how to compare the current item in the aggregation with another cell. I also tried to use the sumifs() function which allows for multiple criteria, but also to no avail. As for my research, I searched on Google but could not find anything, possibly due to my inability to express the requirement in a manner suitable for a google query. Therefore, I have presented the above as a way of explaining my requirement by example.
I hope this helps.
I appreciate that this may not be possible to do with the simple built in formulas. If this is the case, please mention it as that would also be useful to know.
Thank you.
Excel: =SUMPRODUCT(((B:B-A:A)>=1)*(C:C="Y")*(B:B))
Untested, but let me know if it works. Next time, remember to add an example of code/formulas that you have already tried and what error you ran into.
Edit:
Tested it, here is a screenshot of it working with your example data (disregard the fact that my Excel is in spanish)
This works by intersecting both logical tests (that is, performing a logical AND): (B-A)>=1 AND C="Y". Here you can see the result of each logical test and then, finally, where it evaluates to TRUE it returns the value in column B; where it's FALSE, it returns 0. Finally, it sums the values in the result array.

Excel - count text occurrences if string is present in adjacent column

I'm trying to count values in Column A if Column B matches a certain string of text.
assets status
-----------------------------
1 | itemThing | yes
2 | |
3 | itemThing |
4 | |
5 | itemThing | yes
This above example would ideally return 2.
I want to count how many times "item" shows up in column A ONLY if column B says "yes"
I've tried something with =SUMPRODUCT but it doesn't seem to work correctly. It is currently returning 4 when there are 5 matching criteria.
I have =SUMPRODUCT((assets=A1)*(status=B1)) where assets and status are custom names for the column ranges created with Name Manager.
Edit: noticed that is has to be an exact string match for it to count correctly. How do I do partial string matches? e.g. search terms? e.g. match =SUMPRODUCT((assets="*item*")*(status=B1))
Two ways here for your reference:
SUMPRODUCT:
=SUMPRODUCT((ISNUMBER(SEARCH("*itemThing*",assets)))*(status="yes"))
COUNTIFS:
=COUNTIFS(status,"yes",assets,"*itemThing*")
For partial match, use wild card * such as "*itemThing*" and that should do the trick for you.

Return values, based on value of an intersection of a row and column

I want to return a label(s) based on an intersection of a Row and Column equal to "Yes".
| Location |
ID | Tool | Wall | Bin | Toolbox | Count
---+--------+------+-----+---------+-------
1. | Axe | YES | | | 1
2. | Hammer | | | YES | 5
3. | Pliers | | | YES | 2
4. | Nails | | YES | | 500
5. | Hoe | YES | | | 2
6. | Screws | | YES | | 200
7. | Saw | YES | | | 3
What's in Toolbox? (Results wanted)
Axe,Wall, 1
Hammer, Toolbox, 5
Pliers,Toolbox, 2
Nails,Bin, 500
Hoe, Wall, 2
Screws, Bin, 200
Saw, Wall, 3
I also want to be able add Tools and Locations?
Without using VBA, this is going to be a bit of a pain, but workable if you don't mind helper columns. I don't advise trying to do this in a single Array Formula, because text strings are hard to work with in Array formulas. That is - if you have an array of numbers, you can turn that into a single result a lot of ways (MIN, MAX, AVERAGE, SUM, etc.). However with an array of strings, it's harder to work with. Particularly, there's no easy way to concatenate an array of strings.
So instead, use a helper column. Assume column A = toolname, column B = a check for being on the wall, column C = a check for being in the bin, column D for being in the toolbox, and column E for the number available.
FORMATTING SIDE NOTE
First, I will say that I recommend you use TRUE/FALSE instead of "yes"/"no". This is because it is easier to use TRUE / FALSE within Excel formulas. For example, if you want to add A1 + A2 when A3 = "yes", you can do so like this:
=IF(A3="yes",A1+A2)
But if you want to check whether A3 = TRUE, this is simplified:
=IF(A3,A1+A2)
Here, we didn't need to hardcode "yes", because A3 itself will either be TRUE or FALSE. Also consider if you want to make a cell "yes" if A3 > 5, and otherwise be "no". You could do it like this:
=IF(A3>5,"yes","no)
Or, if you used TRUE/FALSE, you could simply use:
=A3>5
However, I'll assume that you keep the formatting you currently have (I would also recommend you just have a single cell that says either "toolbox"/"bin" etc., instead of 4 columns where 1 says "yes", but we'll also assume that this needs to be this way).
BACK TO YOUR QUESTION
Put this in column F, in F2 for the first cell:
=Concatenate(A2," ",INDEX($B$1:$D$1,MATCH("yes",B2:D2,0))," ",E2)
Concatenate combines strings of text into a new single text string. You could also use &; like: A2 & " " etc., but with this many terms, this is likely easier to read. Index looks at your header row 1, and returns the item from the first column which matches "yes" in the current row.
Then put the following in F3 and drag down:
=Concatenate(F2," ", A2," ",INDEX($B$1:$D$1,MATCH("yes",B2:D2,0))," ",E2)
This puts a space in between the line above and the current line. If instead you want to make each row appear after a line-break, use this:
=Concatenate(F2,CHAR(10), A2," ",INDEX($B$1:$D$1,MATCH("yes",B2:D2,0))," ",E2)

Match text from column within a certain cell - Excel

I have a column of few thousand filenames that are not uniform. For instance:
| Column A | Column B |
===============================
| junk_City1_abunc | City1 |
-------------------------------
| nunk_City1_blahb | City1 |
-------------------------------
| small=City2_jdjf | City2 |
-------------------------------
| mozrmcity3_somet | City3 |
I would like to identify the city within the text in column A and return it in Column B.
I've come up with a complex formula that does the trick, but it is difficult to adjust if more cities are added within the filenames in new entries within column A.
Here is an example:
=IF(ISNA(MATCH("*"&$W$3&"*",I248,0)),IF(ISNA(MATCH("*"&$W$4&"*",I248,0)),IF(ISNA(MATCH("*"&$W$5&"*",I248,0)),IF(ISNA(MATCH("*"&$W$6&"*",I248,0)),IF(ISNA(MATCH("*"&$W$7&"*",I248,0)),IF(ISNA(MATCH("*"&$W$8&"*",I248,0)),"Austin","Orlando"),"Las Vegas"),"Chicago"),"Boston"),"Las Angeles"),"National")
It seems like there should be an easier way to do it, but I just can't figure it out.
(To make matters worse, not only am I identifying a city within the filename, I'm looking for other attributes to populate other columns)
Can anyone help?
Use the formula =IFERROR(LOOKUP(1E+100,SEARCH($E$2:$E$11,A2),$E$2:$E$11),A2)
This does *****NOT***** have to be array entered.
Where $E$2:$E$11 is the list of names you want returned and A2 is the cell to test
If no matches are found instead of errors you will just use the full name in column b.
If you want errors or expect to NEVER have then you can just use:
=LOOKUP(1E+100,SEARCH($E$2:$E$11,A2),$E$2:$E$11)
Here's a round about way that works, not all my own work but a mish mash of bits from other sources:
Assuming the sheet is setup as follows:
The formula to use is below, this must be entered using Ctrl+Shift+Enter
=INDEX($C$2:$C$8,MAX(IF(ISERROR(SEARCH($C$2:$C$8,A2)),-1,1)*(ROW($C$2:$C$8)-ROW($C$2)+1)))
Annotated version:
=INDEX([List of search terms],MAX(IF(ISERROR(SEARCH([List of search terms],[Cell to search])),-1,1)*(ROW([List of search terms])-ROW([Cell containing first search term])+1)))

Destination, prefix lookup via Phone number - Excel

I have two tables.
Table one contains: phone number list
Table Two contains: prefix and destination list
I want look up prefix and destination for phone number.
Given below Row data table and result table
Table 01 ( Phone Number List)
Phone Number
------------
12426454407
12865456546
12846546564
14415332165
14426546545
16496564654
16896546564
16413216564
Table 02 (Prefix and Destination List)
PREFIX |COUNTRY
-------+---------------------
1 |Canada_USA_Fixed
1242 |Bahamas
1246 |Barbados
1268 |Antigua
1284 |Tortola
1340 |Virgin Islands - US
1345 |Cayman Island
144153 |Bermuda-Mobile
1473 |Grenada
1649 |Turks and Caicos
1664 |Montserrat
Table 03 (Result)
Phone Number | PREFIX | COUNTRY
--------------+--------+-------------------
12426454407 | 1242 | Bahamas
12865456546 | 1 | Canada_USA_Fixed
12846546564 | 1284 | Tortola
14415332165 | 144153 | Bermuda-Mobile
14426546545 | 1 | Canada_USA_Fixed
16496564654 | 1649 | Turks and Caicos
16896546564 | 1 | Canada_USA_Fixed
16643216564 | 1664 | Montserrat
Lets assume phone numbers are in column A, now in column B you need to extract the prefix. Something like this:
=LEFT(A1, 4)
However your Canada_USA_Fixed creates problems as does the Antigua mobile. I'll let you solve this issue yourself. Start with IF statements.
Now that you have extracted the prefix you can easily use VLOOKUP() to get the country.
Assuming that the longest prefix is 6 digits long you, can add 6 columns (B:G) next to the column with the phone numbers in table 1 (I assume this is column A). In column B you'd show the first 6 characters using =LEFT(A2,6), in the next column you show 5 chars, etc.
Then you add another 6 columns (H:M) , each doing a =MATCH(B2,Table2!A:A,0) to see if this prefix is in the list of prefixes.
Now if any of the 6 potential prefixes match, you'll get the row number of the prefix - else you'll get an #N/A error. Put the following formula in column N: {=INDEX(H2:M2,MATCH(FALSE,ISERROR(H2:M2),0))} - enter the formula as an array formula, i.e. instead of pressing Enter after entering it, press Ctrl-Shift-Enter - you'll see these {} around the formula then, so don't enter those manually!.
Column N now contains the row of the matching prefix or #N/A if no prefix matches. Therefore, put =IF(ISNA(N2,'No matching prefix',INDEX(Table2!B:B,N2)) in the next column and you'll be done.
You could also the above approach with less columns but more complex formulas but I wouldn't recommend it.
I'm also doing longest prefix matches and, like everyone else that Google has turned up, it's also for international phone number prefixes!
My solution is working for my table of 200 prefixes (including world zone 1, ie. having 1 for US/Canada and 1242 for Bahamas, etc).
Firstly you need this array formula (which I'm going to call "X" in the following but you'll want to type out in full)
(LEFT(ValueToFind,LEN(PrefixArray))=PrefixArray)*LEN(PrefixArray)
This uses the trick of multiplying a logical value with an integer so the result is zero if there's no match. You use this find the maximum value in one cell (which I'm calling "MaxValue").
{=MAX(X)}
If MaxValue is more than zero (and therefore some sort of match was found), you can then find the position of the maximum value in your prefix array.
{=MATCH(MaxValue,X,0)}
I've not worried about duplicates here - you can check for them in your PrefixArray separately.
Notes for neophytes:
PrefixArray should be an absolute reference, either stated with lots of $ or as a "named range".
I'm assuming you'll make ValueToFind, MaxValue and the resultant index into PrefixArray as cells on the same row, and therefore have a $ against their column letter but not their row number. This allows easy pasting for lots of rows of ValueToFind.
Array formula are indicated by curly braces, but are entered by typing the text without the curly braces and then hitting Ctrl-Shift-Enter.

Resources