How can I extract words from a cell separately in excel - excel

I am very new to using formulas in excel and I am having some issues. I have four comma separated words in a cell. When I type the words into the cell, I then want to extract each word separately into another cell without the comma.
See screenshot below.
The formula for extracting the first word is working, however the formula for the other 3 words is not and is returning a value error. If there is nothing in cell C7 I don’t want any errors in the other cells either. I will provide the code I have for extracting each of the words, any help appreciated.
Code for extracting first word:
=IF(LEN(C7)=0,"",IF(ISERROR(LEFT($C$7,FIND(",",$C$7)-1)),C7,LEFT($C$7,FIND(",",$C$7)-1)))
Code for extracting second word:
=IF(ISERROR(FIND(",",C7)),"",MID(C7,FIND(",",C7)+1,FIND(",",C7,FIND(",",C7)+1)-FIND(",",C7)-1))
Code for extracting third word:
=MID(C7,FIND(",",C7,FIND(",",C7)+1)+1,FIND(",",C7,FIND(",", C7,FIND(",",C7)+1) +2)-FIND(",", C7,FIND(",",C7)+1)-1)
Code for extracting fourth word:
=RIGHT(C7,LEN(C7)-FIND(",",C7,LEN(E7)+LEN(F7)+LEN(G7)))
One word in cell
No words in cell
When I type in four words it separates them as shown in 3rd screenshot using the formulas above in each cell, which is working ok. However I think I need to edit the formula in F, G and H7 so that if there is one word in C7, it is populated in E7 and F G and H7 are empty(don't show error value either), if there are two words in C7, the first word is populated in E7 and second word in F7 and so on. Apologies if I am not explaining correctly. Also to note, the length of the words in C7 will be variable.

With TEXTSPLIT()(currently available to beta channel Office 365):
Put this in E7
=IFERROR(TRIM(TEXTSPLIT(C7,",")),C7&"")
And it will spill tp the right.
If you are using an older version on a PC:
Put this in E7 and copy over:
=IFERROR(INDEX(FILTERXML("<a><b>"&SUBSTITUTE($C7,",","</b><b>")&"</b></a>","//b"),COLUMN(A1)),"")

Use this User-Defined Function (UDF):
Function Take(invoer As String, index As Integer) As String
temp = Split(invoer, ",")
If UBound(temp) >= index - 1 Then
Take = temp(index - 1)
Else: Take = ""
End If
End Function
And use it as follows in the Excel sheet:

Related

Extracting strings between specific characters in excel and separating them with a comma

Is there a way to extract multiple strings between specific characters in excel and separating them with a comma.
For example:
I am thankful for every help!
The following requires a version of Excel O365 that supports dynamic arrays and the LET function.
If I understand correctly you are looking for something like this.
This formula will list all the positions of all [ in the string using dynamic array functions.
=LET(x,$B$2,
y, SEQUENCE(LEN(x)),
raw, IF(MID(x,y,1)="[",y,""),
filtered, FILTER(raw,raw<>"",""),
filtered)
LET allows you to set names within a formula using parameter pairs. The first is the name; the second is the value of the name. The last parameter is the value returned. In this case, x is set to cell B2. y is set to the array listing the numbers from 1 to the length of x using the dynamic array function SEQUENCE. raw is a list that shows the value of y if that position is [, otherwise it is blank. filtered uses the FILTER function to remove all of the blank rows. filtered is the last argument of the LET function so that is what is returned. If this formula is entered into cell A4 then A4 will show 1, A5 will show 14 and A6 will show 28.
If you then enter a similar formula in B4 replacing [ with ] then the result is {7,21,35} in cells B4, B5 and B6.
Finally, in cell B2 you can enter =TEXTJOIN(", ",TRUE,MID(A2,A4#+1,B4#-A4#-1)). This will return the result you are looking for.
If you are unfamiliar with dynamic arrays, A4# indicates an entire dynamic array starting in cell A4. In this case, it is the same as A4:A6.
More information on Dynamic Arrays
More information on LET
You can't do it with only formulas in Excel 2007. You need VBA UDF then. With Excel O365 having dynamic formula access, this can be done like:
=TEXTJOIN(", ",TRUE,TRIM(LEFT(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A1,"]",REPT(" ",100)),"[","</s><s> ")&"</s></t>","//s[starts-with(., ' ')]"),100)))
Considering that you are using too old version of excel which lacks dynamic array functions and besides too many other useful functions, You may probably have to do a long workaround here.
First using substitute and len You have to find out number of such square parenthesis in each of the row. Thereafter you have to work out the formula of max of such numbers.
Do it like this
Assuming your text-values in A2 for a max of 4 occurrences enter the following formula in B2
=SUBSTITUTE(TRIM(REPLACE(LEFT(A2,FIND("]",A2&"]")-1),1,FIND("[",A2&"["),"")&" "&REPLACE(LEFT(A2,FIND("#",SUBSTITUTE(A2&REPT("]",2),"]","#",2))-1),1,FIND("#",SUBSTITUTE(A2&REPT("[",2),"[","#",2)),"")&" "&REPLACE(LEFT(A2,FIND("#",SUBSTITUTE(A2&REPT("]",3),"]","#",3))-1),1,FIND("#",SUBSTITUTE(A2&REPT("[",3),"[","#",3)),"")&" "&REPLACE(LEFT(A2,FIND("#",SUBSTITUTE(A2&REPT("]",4),"]","#",4))-1),1,FIND("#",SUBSTITUTE(A2&REPT("[",4),"[","#",4)),"")), " ", ", ")
Let's say this is the text in A2
I have a text [123] and some more [4523] and also [552222] how to extract [22]?
This will create a output of 123,4523,552222,22 in B2
There is no "extraction" in your question, just removal. The formula below replaces the unwanted characters with "", thereby removing them. Please try it.
=SUBSTITUTE(SUBSTITUTE(A2,"[",""),"]","")

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.

Searching a letter within a range in excel

I need an excel formula that searches within a range, a cell that contains a word with letter "W" and then multiply the number stored in the cell from the right of it by 2 and display the sum of all this multiplied values in another cell.
Example: Range A4:Y4; B4 contains word "Woo" and C4 contains number "3"; E4 contains word "Wood" and F4 contains number "5"... I need Z4 to contain C4*2+E4*2+...
Please help me with that.
Try this in Z4:
=SUM(SUM(OFFSET($A$4,,IF(IFERROR(FIND("W",$A$4:$Y$4),0)>0,COLUMN($A$4:$Y$4)))))
Hit CTRL + SHIFT + ENTER.
Make sure A4 is not a number (otherwise this formula counts the value of A4 times the number of cells that don't contain "W").
If you want to count cell as well that contain small w's, use SEARCH instead of FIND (SEARCH = case insensitive; FIND = case sensitive).
Keep in mind: OFFSET is a volatile function, i.e. if you have a large datasheet, this might slow down the work a bit.
Found it!
=SUM(IFERROR(2*(LEFT(A4:X4)="W")*B4:Y4,0))
Commit this formula using CTRL+SHIFT+ENTER and not just Enter by itself.

excel first word from cells in other cell

How can I extract the first word of a number of different cells and have each of the first words show together in one other cell separated by comma?
e.g. A1 shows "Firstname1 Lastname1", A2 shows "Firstname2 Lastname2", A3 shows "Firstname3 Lastname3",
I need a formula allowing me to show the following in cell D2 "Firstname1, Firstname2, Firstname3"
I found this solution, which gives me the first word of one cell and shows it in another cell but I don't know how to get the first word of a number of cells and show them all coma separated in another cell
=LEFT(A1,SEARCH(" ",A1)-1)
Excel function to get first word from sentence in other cell
Thanks!
What if instead of just three cells you have an Excel range A1:A100 which has all the names? How would you concatenate in such an instance? Will you type that long a formula?
As Jerry suggested, VBA is Apt for this. But what if you do not want to use VBA or long formulas?
See this example. I am taking 10 cells for the sake of explaining.
Let's say the data looks like this.
Now select the entire column and click on Data~~>Text To Columns
When you click finish, the output will be like this
Now in cell say E4, type this =Transpose(A1:A10). Replace A1:A10 with the actual range. However do not press the Enter key. Press the key F9. You will see that all the first names are now visible.
Simply copy that and press Esc. Now open Notepad and paste it there.
Next delete the { and the }
Next manually replace "," by , and you will get what you wanted.
=LEFT(A1,SEARCH(" ",A1)-1) &", "&LEFT(A2,SEARCH(" ",A2)-1)&", "&LEFT(A3,SEARCH(" ",A3)-1)
using your current formula, =LEFT(A1,SEARCH(" ",A1)-1), you can add the value of subsequent cells to the previous calculation, and build the comma separated values.
in B1, we have your original formula, =LEFT(A1,SEARCH(" ",A1)-1)
in B2 we concatenate the previous result, and add on the comma and the new word:
=B1 & "," & LEFT(A2,SEARCH(" ",A2)-1)
copy this to B3 (and on) and you will slowly see the full list getting created.
The last value is the one you want, so, in C1, put the formula
=OFFSET(B1,COUNTA(B:B)-1,0)
(and hide column B so it looks better)

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