How to extract specific text in excel? - excel

How do I get a specific text only like "LHS" or "RHS"?
Thank you for helping.

Provided that there are no other 3 character combinations ending with HS you can use: =MID(A1,SEARCH("?HS",A1)+1,3)
If you want to be sure to only find LHS or RHS use: =MID(A1,IFERROR(SEARCH("LHS",A1),SEARCH("RHS",A1))+1,3)
(Change A1 in the formula to the cell you like to search in)
Edit: Left out spaces in front and after search value to find result at end of beginning of text
Edit2: Added the spaces again, implementing Chris Neilsen's workaround:
=MID(" "&A1&" ",IFERROR(SEARCH(" LHS "," "&A1&" "),SEARCH(" RHS "," "&A1&" "))+1,3)

In B2, formula copied down :
=MID(A2,MIN(SEARCH({"lhs","rhs"},A2&"lhsrhs")),3)

Related

How to find the text/number between 2 characters, the last being a non-unique comma using Excel formulas?

The challenge is to find the number between "produto_id:" and the last character, which is sometimes a "]" or a " , ". When it's a "]", it does extract the text, but it doesn't when it's a comma.
The cell value:
linha:2,quantidade:2,preco_cheio:19.9000,preco_promocional:null,preco_venda:19.9000,preco_custo:null,produto_id:27319353,
The formula I'm using, which works when the last character is a ]:
=IFERROR(VALUE(MID(LEFT($AV9;SEARCH("]";$AV9)-1);SEARCH("produto_id:";$AV9)+11;LEN($AV9)));"")
For the occurrences ending in a comma:
=IFERROR(VALUE(MID(LEFT($AV9;SEARCH(",";$AV9)-1);SEARCH("produto_id:";$AV9)+11;LEN($AV9)));"")
I'd appreciate if you could give it a glance and let me know where I'm failing.
Thanks.
Antonio here is a solution for your problem:
Portuguese formula:
=SUBSTITUIR(SUBSTITUIR(EXT.TEXTO(A2;LOCALIZAR("produto_id:";A2;1)+11;NÚM.CARACT(A2)-(LOCALIZAR("produto_id:";A2;1)+10));",";"");"]";"")
English formula:
=REPLACE(REPLACE(MID(A2;SEARCH("produto_id:";A2;1)+11;LEN(A2)-(SEARCH("produto_id:";A2;1)+10));",";"");"]";"")
replace A2 for your cell.
Give a try on below FILTERXML() formula.
=FILTERXML("<t><s>"&SUBSTITUTE(FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A1,",","</s><s>"),",","</s><s>")&"</s></t>","//s[contains(., 'produto_id:')]"),":","</s><s>") & "</s></t>","//s[last()]")
In B1, formula copied down :
=MID(A1,SEARCH("produto_id",A1)+11,LEN(A1)-SEARCH("produto_id",A1)-11)

How can I remove ONLY leading and trailing spaces while leaving spaces in between words alone with an excel formula?

In excel, TRIM() will remove all spaces before and after text, while also removing any duplicate spaces in between words.
Is there a formula or combination thereof that will do the same as TRIM() but leave spaces between words as-is?
In the following example, I'm looking for a formula that will accomplish that of the fictitious formula "WXYZ":
TRIM(" Omicron Persei 8 ") = "Omicron Persei 8"
WXYZ(" Omicron Persei 8 ") = "Omicron Persei 8"
Note that I've read somewhere that TRIM() in VBA will work like that of WXYZ above. However, I'm looking for a formula solution.
I believe this should work (assuming your string is located at A1):
=MID(A1,
FIND(LEFT(TRIM(A1),1),A1),
(LEN(A1)-MATCH(RIGHT(TRIM(A1),1),INDEX(MID(A1,LEN(A1)-ROW(INDIRECT("1:"&LEN(A1)))+1,1),0),0)-FIND(LEFT(TRIM(A1),1),A1)+2)
FIND(LEFT(TRIM(A1),1),A1) returns the location of the first non-space character in the string
MATCH(RIGHT(TRIM(A1),1),INDEX(MID(A1,LEN(A1)-ROW(INDIRECT("1:"&LEN(A1)))+1,1),0),0) returns the location of the last non-space character in the string from right-to-left.
How would this look in Excel 365? A bit easier I think with let, sequence and xmatch but not particularly short:
=IFERROR(LET(seq,SEQUENCE(LEN(A2)),
array,MID(A2,seq,1),
start,XMATCH(TRUE,array<>" "),
finish,XMATCH(TRUE,array<>" ",0,-1),
MID(A2,start,finish-start+1)),"")
Just to add to all the valuable content:
Formula in B1:
=LET(x,TEXTSPLIT(A2," ",,1),TEXTJOIN(DROP(DROP(TEXTSPLIT(" "&A2&" ",x),,1),,-1),,x))
A Formula Array could also be used.
Assuming the string is located at A1 enter this Formula Array in B2. It's highly suggested to ensure this part of the formula ROW(B:B) refers always to the same column were the formula is located (column B in this case), this is in order to avoid the formula returning an error if the column to which it refers is deleted.
=MID($A1,
FIND(LEFT(TRIM($A1),1),$A1),
1+MAX(ROW(B:B)*(ROW(B:B)<=LEN($A1))*(MID($A1,ROW(B:B),1)<>" "))
-FIND(LEFT(TRIM($A1),1),$A1))
FormulaArrays are entered pressing [Ctrl] + [Shift] + [Enter] simultaneously, you shall see { and } around the formula if entered correctly
As regards the formula provided by #Aakash I suggest to replace the INDIRECT function in this part:
-ROW(INDIRECT("1:"&LEN($A7)))
with this:
-ROW(B:B)
So the formula will become Non-Volatile:
=MID($A1,
FIND(LEFT(TRIM($A1),1),$A1),
(LEN($A1)-MATCH(RIGHT(TRIM($A1),1),INDEX(MID($A1,LEN($A1)-ROW(B:B)+1,1),0),0)
-FIND(LEFT(TRIM($A1),1),$A1)+2))

Excel: Using text functions to find the middle

I want to extract Moss Ariel and Murphy from the following strings using text functions in Excel.
Gabriela Moss669.11695-5000-53420000000-1232
Connie Ariel1025.11695-2004-51490000000-1231
Kelly Murphy1040.58695-2200-50630000000-1235
I have a basic grasp with using text functions, but this one has stumped me. Any help is greatly appreciated.
I need to make a formula such that it requires no changes copying it down the list of records.
This would be easier and shorter in Google Sheets or with VBA UDF, but here is another array formula solution (enter it with Ctrl + Shift + Enter) from https://exceljet.net/formula/split-text-and-numbers
=MID(A1,FIND(" ",A1)+1,MIN(FIND(ROW($1:$10)-1,A1&"0123456789"))-FIND(" ",A1)-1)
This might work without array formula:
=MID(A1,FIND(" ",A1)+1,MIN(FIND({0,1,2,3,4,5,6,7,8,9,"."},A1))-FIND(" ",A1)-1)
Say you data begins in A1
First extract the full name with the following formula
[B1] = {LEFT(A1,MATCH(1,--ISNUMBER(--MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1)),0)-1)}
This is an array formula, press Ctrl+Shift+Enter after typing formula in B1. Then fill down the formula to the rest of the rows.
Next, simply extract the last name from the full name
[C1] = RIGHT(B1,LEN(B1)-SEARCH(" ",B1))
If you prefer to do it in-one-go here is the formula
[B1] = {MID(A1,SEARCH(" ",A1)+1,MATCH(1,--ISNUMBER(--MID(A1,ROW(INDIRECT("A1:A"&LEN(A1))),1)),0)-1-SEARCH(" ",A1))}
You can use the below formula as it is, no need for array formula to achieve this.
=MID(A1,SEARCH(" ",A1,1)+1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))-SEARCH(" ",A1,1)-1)
You need to combine the functions instr and mid. instr returns the index in a string of the first appearance of a sub-string (or a single character). mid returns a sub-string starting at a given position and with a length of the extracted sub-string. In your case, something like:
MID(MyString,Index(MyString,"Moss"),Len("Moss"))
where MyString holds your source string.

Nested IFs in function not working

Hi, I'm trying to input a function in C2 in order to assign a numerical value to the minutes given in Column B. The criteria for this can be seen in the image above (starting at G1).
I have tried using a formula I copied from a similar situation but is not working:
=IF(B2<=$A$2,5,IF(B2<=$A$3,4,IF(B2<=$A$4,3,IF(B2<=$A$5,2,1))))
Any help would be appreciated, thanks
The suggestion by #Jeeped above will simplify the formulas needed. If you have to keep the 'A' and 'B' cells as they are listed above, you have to extract the number from the 'x min' format and convert the 'x' to a number so it can be compared (I assume a " " exists after the number. Could search for " min" as well).
=VALUE(LEFT(A2,SEARCH(" ",A2)-1))
Using the above, if A2 = '60 min', the formula will produce a '60'.
Now that the cell contents can be treated as numbers, the comparisons can be made. Formula for C2:
=IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$2,SEARCH(" ",$A$2)-1)),5,
IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$3,SEARCH(" ",$A$3)-1)),4,
IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$4,SEARCH(" ",$A$4)-1)),3,
IF(VALUE(LEFT(B2,SEARCH(" ",B2)-1))<=VALUE(LEFT($A$5,SEARCH(" ",$A$5)-1)),2,1))))
This is ugly, but works given the original question.
Try this formula (in this case for C2):
=SUM((B2<=$A$2:$A$5)*1)+1
It is important to use it as array formula. So after typing or inserting this formula to your cell don't just commit with Return but hit Ctrl+Shift+Return. If you did it right, your formula will be surrounded by curly brackets in formula bar:
{=SUM((B2<=$A$2:$A$5)*1)+1}

Counting number of spaces before a string in Excel

A program that exports to Excel creates a file with an indented list in a single column like this:
Column A
First Text
Second Text
Third Text
Fourth Text
Fifth Text
How can I create a function in excel that counts the number of white spaces before the string of text?
So as to return: 1 for the first text row and 3 for the for the thirst row, etc in this example.
Preferably seeking a non-VBA solution.
TRIM doesn't help here because it removes double spaces also between words.
The main idea is to find the FIRST letter in the trimmed string and find its position in the original string:
=FIND(LEFT(TRIM(A1),1),A1)-1
You can try this function in Ms Excel itself:
=LEN(A1)-LEN(SUBSTITUTE(A1," ",""))
This would apply if the results are in a single cell. If it is for a whole row/column, just drag the formula accordingly.
Try below:
=FIND(" ",A1,1)-1
It calculates the position of the first found whitespace character in a cell and reduces it by 1 to reflect number of characters before that position.
As per http://www.mrexcel.com/forum/excel-questions/61485-counting-spaces.html, you may try:
=LEN(Cell)-LEN(SUBSTITUTE(Cell," ",""))
where Cell is your target cell (i.e. A1, B1, D3, etc.).
My example:
B8: =LEN(F8)-LEN(SUBSTITUTE(F8," ",""))
F8: [ this is a test ]
produces 4 in B8.
The above method will count spaces before the string if any were inserted, between individual words and after the string, if any were inserted. It won't count available space that does not have an actual white space character. So, if I inserted two spaces after test in the above example, the total count would be raised to 6.
As has been pointed out in the other answers, you can't really use TRIM or SUBSTITUTE as potential spaces in between words or at the end will give you the wrong result.
However, this formula will work:
=MATCH(TRUE,MID(A1,COLUMN($A$1:$J$1),1)<>" ",0)-1
You need to enter it as an array formula, i.e. press Ctrl-Shift-Enter instead of Enter.
In case you expect more than 10 spaces, replace the $J with a column letter further down in the alphabet!
Here's my solution. If the left 5 characters equals "_____" (5 blank spaces), then return 5, else look for 4 spaces, and so on.
=IF(LEFT(B1,5)=" ",5,IF(LEFT(B1,4)=" ",4,IF(LEFT(B1,3)=" ",3,IF(LEFT(B1,2)=" ",2,1))))
You almost got it with LEN + TRIM in answers before, you only need to combine both:
=LEN(Cell)-LEN(TRIM(Cell))
If it is Indented you could create a Personal Function like this:
Function IndentLevel(Cell As Range)
'This function returns the indentation of a cell content
Application.Volatile
'With "Application.Volatile" you can make sure, that the function will be
recalculated once the worksheet is recalculated
'for example, when you press F9 (Windows) or press enter in a cell
IndentLevel = Cell.IndentLevel
'Return the IndentLevel
End Function
This will work only if it is Indented, you can see this property in the Cell Format -> Alignment.
After This you could see the Indentation Level.

Resources