Excel - extracting text between parentheses after finding certain text - excel

I have tried searching for an answer but can't seem to find the exact solution. I am trying to extract text between two parentheses starting at a certain string of text. I.e. the cell contains the following – ABC (12.3%) DEFGH (18.1%) IJKL (17.2%). I want to have a reference cell saying "ABC" then the cell below return the % number in between parentheses following ABC.
The current formula I am using is the below, where G6 is the full line of text and I5 is the reference cell "ABC":
=MID(G6,FIND(I5,G6)+FIND("(",G6),FIND("(",G6)+1+FIND(")",G6)-FIND("(",G6)-7)
This will work when the Input is 3 characters long (ABC), but won't work when the string text is a different length.
Can someone help me create a formula where I can pull the % number regardless of how many characters there are?

With the big string in A1 and ABC in B1, try:
=LEFT(MID(A1,FIND(B1,A1)+LEN(B1)+2,9999),FIND(")",MID(A1,FIND(B1,A1)+LEN(B1)+2,9999))-1)
what is going on:
The core of the formula: MID(D1,FIND(E1,D1)+LEN(E1)+2,9999) discards the front end of the string and returns:12.3%) DEFGH (18.1%) IJKL (17.2%).The enclosing part discards the closing parens and every that follows.

If you are trying to separate all the different strings, then it might be best to split it up.
String Splitting
Cell A1: 'The current String'
Cell B1: =SEARCH("(",A2)
Cell C1: =SEARCH(")",A2)
Cell D1: =MID($A$2,1,B2-1)
Cell E1: =MID($A$2,B2+1,(C2-B2)-1)
Cell F1: =SEARCH("(",A2,12)
Cell G1: =SEARCH(")",A2,12)
Cell H1: =MID($A$2,C2+1,(F2-C2)-1)
Cell I1: =MID($A$2,F2+1,(G2-F2)-1)
Cell J1: =SEARCH("(",A2,26)
Cell K1: =SEARCH(")",A2,26)
Cell L1: =MID($A$2,G2+1,(J2-G2)-1)
Cell M1: =MID($A$2,J2+1,(K2-J2)-1)
B1 & C1 will search for the first appearance of "(" & ")".
D1 & E1 will then use those numbers in B1 & C1 to find the text you're searching for.
F1 & G1 will search for the second appearance of "(" & ")"
H1 & I1 will then use those numbers in F1 & G1 to find the text you're searching for.
J1 & K1 will search for the third appearance of "(" & ")"
L1 & M1 will then use those numbers in J1 & K1 to find the text you're searching for.
This process breaks it down for every piece of string. In the picture I attached I also added additional fields for trimming the results to eliminate any blank spaces.

Related

Excel - using cells in equations

Is there any way to use cells of excel in my mathematical equations? I'd like a way to just change the numbers and the equations automaticlly update with them. I tried concatenating strings but it's not that pretty. Or maybe is there any other way to easily show equations step by step in excel?
You say that concatenation of strings is not that pretty, so I assume you have done something like:
in cell A1 : "Hello"
in cell A2 : "World"
in cell A3 : =A1&A2
And in cell A3, you see "HelloWorld", without any space (which is indeed not pretty).
You can solve this in two ways in A3:
=A1&" "&A2
=TEXTJOIN(A1:A2, " ")
(I'm not entirely sure about the syntax of the second one.)
In the first formula, you concatenate both cells A1 and A2 and you specifically mention to put a space between those two.
In the second you do the same, but you mention that the space must be used everywhere.
The difference can be seen in following example:
In cell A1: "Hello"
In cell A2: "Lovely"
In cell A3: "World"
In cell A4, you can put: =A1&" "&A2&" "&A" for getting "Hello Lovely World": you need to mention the space character twice.
In cell A4, you can put: =TEXTJOIN(A1:A3, " ") for getting the same result. As you see you only need to mention the space character once.
Is this what you are looking for?

Excel - breaking up a cell

I have part numbers with dashs and numbers for organization purpose, and I was wondering if there's a way I could de-concatenate the string into nearby cells with a formula or two and not go through Data > Text to Columns. Any idea?
Here's a demo of a formula-based approach. The formulas will work in Excel.
https://docs.google.com/spreadsheets/d/1LXtKOsxzo1J2-D1e4e_QUqxUug191GHJ5exWSWcoS0s/edit?usp=sharing
It works like this:
A1 contains the string you want to break up, say 12424-22778-3432626-442-52523262.
B2 contains this formula: =FIND("-",$A1,1). This will find us the first occurrence of - in A1.
B3 contains this formula: =FIND("-",$A1,B1+1). This will find us the next occurrence of - in A1 (it starts looking for - from the position following what we found in B2.
B4... can be copied over from B3. If you copy the cell rather than the formula, the formula will automatically change the reference to B1 to C1 and up. In the example, it's copied over all the way to E1.
F1 contains =mid($A1,1,B1-1). This gets us the string from the first char to the B1-1th char, i.e.: start to first occurrence of -, but without the -.
G1 contains =mid($A1,B1+1,C1-B1-1). You can copy this over up to the second to last cell. The gets us the string from the previous - (+1, i.e. without the -) to the next - (-1, i.e. without the -).
J1 is the last cell and contains =mid($A1,$E1+1,len($A1)). It works the same as the previous formula, but goes up to the end of the string in A1.

Google Sheets/Excel formula to reduce comma-separated lists to a particular subset

In a database extract, each row has a cell containing a comma-separated list of tags. I need a formula to filter each of these cells down to a particular subset.
I've got a rudimentary working version but it's horrifically verbose.
The tag list is in column C.
The subset of tags I want to keep is currently in A1:A10 on a separate sheet (called Subset).
My rudimentary version concatenates the same sub-formula once for each cell in A1:A10. Each sub-formula tests if FIND(SubsetTag, TagList) returns a number and, if so, outputs the SubsetTag followed by ", " to build up a string containing all the matches e.g.
=IF(ISNUMBER(FIND(Subset!A$1,C2)),Subset!A$1&", ","") & IF(ISNUMBER(FIND(Subset!A$2,C2)),Subset!A$2&", ","") & IF(ISNUMBER(FIND(Subset!A$3,C2)),Subset!A$3&", ","") & IF(ISNUMBER(FIND(Subset!A$4,C2)),Subset!A$4&", ","") & IF(ISNUMBER(FIND(Subset!A$5,C2)),Subset!A$5&", ","") & IF(ISNUMBER(FIND(Subset!A$6,C2)),Subset!A$6&", ","") & IF(ISNUMBER(FIND(Subset!A$7,C2)),Subset!A$7&", ","") & IF(ISNUMBER(FIND(Subset!A$8,C2)),Subset!A$8&", ","") & IF(ISNUMBER(FIND(Subset!A$9,C2)),Subset!A$9&", ","") & IF(ISNUMBER(FIND(Subset!A$10,C2)),Subset!A$10&", ","")
Is there a good way to do this using formulas? (I'd like to avoid scripts)
Ideally I'd like to generalise the formula so it doesn't have to be updated when the subset grows beyond A1:A10 and would like to avoid the trailing comma :)
You can do this with an array formula combined with CONCAT()
=LEFT(CONCAT(IF(ISNUMBER(FIND(M8:M10,J8)),M8:N10,"")),LEN(CONCAT(IF(ISNUMBER(FIND(M8:M10,J8)),M8:N10,"")))-2)
where:
M8:M10 is the list of tags,
N8:N10 is a list of the string ", "
J8 is the string you are searching
Make sure you press Control + Shift + Enter when you finish the formula.
The LEFT() part is to remove the final comma.
Here is a Google Sheets solution, using regexmatch:
=join(",", filter(Subset!A1:A10, regexmatch(C2, "\b" & Subset!A1:A10 & "\b")))
The set in range A1:A10 is filtered down to those tags for which the string C2 matches a regular expression of the form \btag\b, meaning C2 must contain the tag as a separate word (\b is word boundary). The filtered array is then joined into a new comma-separated string.
If none of the tags are contained in C2, the formula returns error message #N/A. If you prefer empty output in this case, wrap the formula in =iferror().

Excel search for a string and how many times a charactor is in the cell

I have a list in excel that contain location but some cell have multiple locations separated by " _ " character for Example "_ Location1 _ Location 2" When there are only 1 location I can use Sumif to search for string and add the numbers next to the cell as shown here
My problem is not searching for a string but searching for a character in a list and finding how many there are in the cell it find I was going to add the formula in a different cell then the ones shown above
example formula NOT REAL
=sum(Sumif($A$4:$A$250,"* ~ Location1*",$C$4:$C$250)/Search($A$4:$A$250," ~ "))
I know search does work like this but as an example code this is what I imagine
to find the sum of each location use this array formula:
=SUMPRODUCT((ISNUMBER(SEARCH(D4,$A$4:$A$9)))*($C$4:$C$9/(IF(ISNUMBER(FIND("_",$A$4:$A$9)),LEN($A$4:$A$9)-LEN(SUBSTITUTE($A$4:$A$9,"_","")),1))))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter. If done correctly then Excel will put {} around the formula.
Enter the formula in E4, Hit Ctrl-Shift-Enter, then copy/fill down.
Then to get the total for all simply sum the rows above.
To do it with regular formulas:
You will need a helper column with the following formula:
=C4/(IF(ISNUMBER(FIND("_",A4)),LEN(A4)-LEN(SUBSTITUTE(A4,"_","")),1))
I put mine in Column G.
Then we can use a simple SUMIF():
=SUMIF(A:A,"*" & D4 & "*",G:G)

Excel text formula to extract words separated by semicolons in a field

A field in Excel contains words separated by semicolons, e.g.:
A1 = save;the;national;treasure;for;good
How can I apply Excel text formulas to produce separate words from this field in another fields? E.g.:
A2 should contain a formula to get the first word ("save")
A3 should contain a (different) formula to get the second word ("the")
etc.
However these formulas should hold good even when the value in A1 changes, e.g. if the value of A1 is changed to
A1 = hello;there;how;are;you
Any help in this respect will be highly appreciated.
(The problem is writing a function of my own is not allowed in this case, I have to use original functions like find, search, mid, etc.)
You can create a VBA function to split the fields from this example:
Function ExtractElement(str, n, sepChar)
' Returns the nth element from a string,
' using a specified separator character
Dim x As Variant
x = Split(str, sepChar)
If n > 0 And n - 1 <= UBound(x) Then
ExtractElement = x(n - 1)
Else
ExtractElement = ""
End If
End Function
Then the A2 formula would be: =ExtractElement(A1, 1, ";") and A3 would be: =ExtractElement(A1, 2, ";") and so on
If you have your text to parse in A1 then the following formulas should work
In A2 enter the formula
=IF(ISERROR(LEFT(A1,FIND(";",A1)-1)),A1,LEFT(A1,FIND(";",A1)-1))
In B2 enter the formula
=IF(ISERROR(RIGHT(A1,LEN(A1)-FIND(";",A1))),"",RIGHT(A1,LEN(A1)-FIND(";",A1)))
You can then copy those down as far as you need. Column A grabs the left most word, and Column B displays the remaining string to be parsed. If it runs out of words to parse the formula will display a blank. Column B can also be hidden.
If you can use intermediate formulae, then this will work:
A1 -- save;the;national;treasure;for;good
B1 -- blank
C1 -- =IFERROR(FIND(";",$A1,1+(B1)),LEN($A1)+1)
copy C1 into D1:H1
C2 -- =MID($A1,B1+1,(C1-B1)-1)
copy C2 into D2:H2
Row 1 will display the position of each semi-colon in A1, because it starts looking in the string one character past the semi-colon found in the previous cell.
eg cell E1 searches for a semi-colon in A1 starting at D1+1 =10.
The iferror statement in C1:H1 traps the error which will occur when the search finds no further semi-colons, and returns the full length of string A1, plus 1 for an imaginary semi-colon at the end.
B1 needs to be blank to create an initial zero.
Cells C2:H2 then use the Mid function to copy the section of the A1 string starting one character after the value in each of B1:G1, with length (C1-B1)-1, (d1-c1)-1 etc (minus one to cut out the semi-colon itself)
You should get: 5, 9,18,27,31,36 in Row 1, and beneath those cells the individual words.
Hope this helps.

Resources