How to replace string with the number - excel

I want to replace the comma separated letters in a cell with the numbers. I've used the=LOOKUP(A1,{"a","b","c","d","e"},{1,2,3,4,5}) function but it's only working for the single letter.
for example: my desired output:
a => 1

If you have Excel 2016+ with the TEXTJOIN function, you can use the array formula:
=TEXTJOIN(",",TRUE,CODE(UPPER(FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s")))-64)
Since this is an array formula, you need to "confirm" it by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar
Algorithm:
Convert comma-separated string to XML: "<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>"
Use FILTERXML to convert the XML to an array of strings
UPPER to make case insensitive
CODE(…) - 64 to convert to a series of numbers related to the letter)
TEXTJOIN to put the result together

Related

Trim values in array?

The match excel function takes an array or table as second parameter. Is there a way to trim the values of the array before feeding to match function?
I am looking for something like:
=MATCH("bat", TRIM(A1:A12), 0)
Thanks!
It will work as an array formula:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.

Excel 2013 find function for numbers

I am trying to understand why this formula gives syntax error in Excel 2013:
=FIND({0,1,2,3,4,5,6,7,8,9};A1&"0123456789")
Worked well when looking for one number only:
=FIND({0};A1&"0123456789")
But having issue when trying to look for multiple value:
{0,1,2,3,4,5,6,7,8,9}
To get the position of the first digit in a string, use:
=MIN(FIND({"0","1","2","3","4","5","6","7","8","9"},A1&"0123456789"))
(which will return a 3 in the example below). To get the value of the first digit in a string use:
=MID(A1,MIN(FIND({"0","1","2","3","4","5","6","7","8","9"},A1&"0123456789")),1)
(your version of Excel may require ; rather than ,)
EDIT#1:
If you wish to parse a string and extract all the digits, use the array formula:
=--MID(SUMPRODUCT(--MID("01"&A1,SMALL((ROW($1:$300)-1)*ISNUMBER(-MID("01"&A1,ROW($1:$300),1)),ROW($1:$300))+1,1),10^(300-ROW($1:$300))),2,300)
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

How to extract parsed data from once cell to another

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!

Excel find text value in string

I have a string such as K68272CAA6A1
And need to do that, formula will pass the first character (I mean string will be 68272CAA6A1 in mind) and formula will find the first text character. And cell value will be 7. Because first text character is "C" and it's the 7th character of my string (include "K" character).
And after that I'll split rest of them. But I'm confused about this issue.
If I understand you correctly, you are looking for the position of the 2nd letter in your string. That number is given by the following array-entered formula.
To enter an array formula, hold down ctrl+shift while hitting Enter. If you do this correctly, in the Formula Bar you will see braces {...} around the formula:
=MATCH(FALSE,ISNUMBER(MID(A1,ROW(INDIRECT("2:99")),1)/1),0)+1
The 99 just needs to be some number larger than the length of your longest string.
If I understood you correctly, a formula that implements this functionality (assuming cell A1 = K68272CAA6A1 and B1 = K) would be:
=FIND(RIGHT(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(RIGHT(A1,LEN(A1)-FIND(B1,A1)),"1",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9",""),1),RIGHT(A1,LEN(A1)-FIND(B1,A1)))-1
The long sequence of substitute is there to remove the numbers (I couldn't find a specific formula to remove them).
This gigantic formula for your example would simply give the answer 6.
To get the strings separated as you want all you need to do is =LEFT(A1,D1) supposing the long formula is on D1 and =RIGHT(A1,D1), which in your example would yield respectively K68272 and CAA6A1

Adding numbers enclosed in characters

I am trying to add numbers in excel which contain characters.
For example, I want to add
rs30/-
rs40/-
rs45/-
I want result as rs115/-
I tried
=SUM(IF(ISNUMBER(--A1:A3),--A1:A3))
but that gives zero.
You can use MID to extract the number text from the character string, then VALUE to convert that text to an actual numerical value, then add those using SUM, then use & to concatenate with the characters you want before and after the result.
Example:
="rs" & SUM(VALUE(MID(A1:A3,3,LEN(A1:A3)-4))) & "/-"
entered as an array formula using Ctrl Shift Enter.
You might prefer to strip out rs and /- (with Find & Select), then format as:
"rs"0"/-"

Resources