Related
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)
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))
Given a spreadsheet cell containing a string that consists of a hyphenated series of character segments, I need to extract the last segment.
For example, consider column A containing data strings like XX-XXX-X-XX-XX-G10, where X denotes any character. What formula would I need to place in column B to get G10 as a result?
A B
1 XX-XXX-X-XX-XX-G10 G10
I'm looking for a formula that could work in in Libre Office Calc, Open Office Calc, MS Excel, or Google Sheets.
Another possibility in LO Calc is to use the general purpose regular expression macro shown here: https://superuser.com/a/1072196/541756. Then the cell formula would be similar to JPV's answer:
=REFIND(A1,"([^-]+$)")
If you are using google sheets, regexextract would be possible too:
=REGEXEXTRACT(A1, "[^-]+$")
In LibreOffice Calc and OpenOffice Calc, you can use a regular expression to determine the position of the text after the last - character:
=SEARCH("-[:alnum:]+$";A1)
will return 15 if A1 contains XX-XXX-X-XX-XX-G10.
Now, you can use this value to get the text "behind" that position, using the RIGHT() function:
=RIGHT(A1;LEN(A1)-SEARCH("-[:alnum:]+$";A1))
Split up on multiple lines:
=RIGHT( ' return text beginning from the right...
A1; ' of cell A1 ...
LEN(A1) ' start at lenght(A1) = 18
- ' minus ...
SEARCH( ' position ...
"-[:alnum:]+$" ' of last "-" ...
;A1 ' in cell A1 = 15 ==> last three characters
)
)
It appears that you want the characters that appear at the end of a string, to the right of the last instance of a hyphen character, "-".
This formula, adapted from here, works in Excel, *Calc & Google Sheets:
=TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),LEN(A1)))
Explanation:
SUBSTITUTE(A1,"-",new_string) will find each hyphen ("-") in the original string from cell A1 and replace it with a new_string.
REPT(" ",LEN(A1)) is a string of repeated space characters (" "), the same length as the original string in cell A1.
TRIM(RIGHT(string,count)) will get the right-most count characters, and trim off leading and trailing spaces. Since the string was previously padded out by replacing hyphens with spaces, and count is the same LEN(A1) used for that padding, the last count characters consists of a bunch of spaces followed by whatever followed the last hyphen!
In Google Sheets, an alternative approach is to use the SPLIT function to break the value from column A into an array, then select the last element. (Excel-VBA has a split() function, so you could make this work in Excel by writing VBA code to provide it as a custom function.)
=INDEX(SPLIT(A1,"-"),0,COUNTA(SPLIT(A1,"-")))
I found simply solution:
=RIGHT(A1;3)
that gives me G10 as the result too! It works because COL A always have 3 chars at the end!
Can you tell me if I want to find the last character in Excel based on condition
let say last character if it is A then replace it with X, or if it is b then replace it with Z.
I want to do it with formula
If your value is in A1 cell, then try applying the following formula in B1 cell.
Formula
=IF(RIGHT(A1,1)="a", LEFT(A1,LEN(A1)-1) &"x", IF(RIGHT(A1,1)="b",LEFT(A1,LEN(A1)-1) & "z",A1))
If you are looking for last character to be exactly A, then try the following formula.
Formula
=IF(EXACT(RIGHT(A1,1),"A"), LEFT(A1,LEN(A1)-1) &"X", IF(EXACT(RIGHT(A1,1),"B"),LEFT(A1,LEN(A1)-1) & "Z",A1))
You can use this formula. See the example sheet to understand the values.
In Example:
"A2": Original Text
"B2": Result
=IF(RIGHT(A2,1)="A",REPLACE(A2,LEN(A2),1,"X"),IF(RIGHT(A2,1)="B",REPLACE(A2,LEN(A2),1,"Z"),A2))
I am looking up cell C2 in range A1:B9 and returning the value from 2nd column.
The cell value can be text or text and special characters eg. CL,CL*,C-L etc.
The range has space in front or after the characters so I guess trim required.
I used the below formula which include "~"& before and after C2 to let excel read the special character as they are. However it is not working as I expected. Can anyone point out the issue and solution?
=VLOOKUP("~"&C2&"~",TRIM($A$1:$B$9),2,FALSE)
i.e.
I am looking up Cl* (C2) and the range of lookup is like below, I expect the formula to return Cl* but it returns ClG:
ClG ClG
Cl*? C?*
GlCl? Gl?
Cl* Cl*
GlCl GlC
CataclyV* CV*
Cloud Cld
*inCl *iC
GinCl GiC
Try this one:
=VLOOKUP(SUBSTITUTE(SUBSTITUTE(C2,"?","~?"),"*","~*"),TRIM($A$1:$B$9),2,0)
and press CTRL+SHIFT+ENTER to evaluate it
UPDATE:
similar to another answer, you can use this one without array enrty (returns last appearence of cell C2 in range A1:A9):
=LOOKUP(2,1/(TRIM(A1:A9)=C2),B1:B9)
You can try this array formula:
=INDEX(B1:B9;MATCH(1;FIND(C2;TRIM(A1:A9));0))
You need Ctrl Shift Enter to enter the formula
Depending on your regional settings you may need to replace ";" by ","
Yes you are right replace by:
=INDEX(B1:B9;MATCH(1;1*(C2=TRIM(A1:A9));0))