Excel formula to remove number + character from text - excel

Is is possible to remove to achieve the following using formula? I want to get rid of the numbers at the end. Thanks.

This is a possibility with the following assumptions:
The delimiter to differentiate between number and rest of string can be both - or _
There always is a number to be taken off from the string
The formula used in B2:
=LEFT(A2,LEN(A2)-LEN(TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE(A2,"_","-"),"-",REPT(" ",LEN(A2))),LEN(A2))))-1)
Drag down...

You need to use the LEFT() function to get rid of text at the end. Sample syntax:
LEFT(cell_id,LEN(cell_id)-num_chars)
For example, if you wanted to remove the last 3 characters from cell A4:
LEFT(A4,LEN(A4)-3)
However, in your case, it looks like you want to get rid of text after the last occurrence of a certain delimiter/separator - that being "-" or "_", so try these two:
LEFT(A4,FIND("#",SUBSTITUTE(A1,"-","#",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))-1)
and
LEFT(A4,FIND("#",SUBSTITUTE(A1,"_","#",LEN(A1)-LEN(SUBSTITUTE(A1,"_",""))))-1)

Related

Unable to convert text to Numbers

I've been given an excel to import on Database, it was exported from an Access DB. in the excel there's a column type_class, in one excel it's good(sheet1), but on another excel which I moved to sheet2 to make VLOOKUP function, I can't tell whether it's a text or a number column from the first sight. the upper-left green-thing is not showing on all cells. but, using ISTEXT function result in text. below is the original column without any changes or formatting, as well as ISTEXT result.
when I use the column in a VLOOKUB function to transfer the Name to the first sheet, only (1010, 1101, 1102,....), hence the cells with the green-mark on the upper-left corner.
I can easly format the key in sheet1 using text-to-columns, cell formatting, and any other way.
but I cannot change the column in sheet2, I tried:
Text-to-Columns
Cell Formatting
VALUE(text), CLEAN(text), TRIM(text), TRIM(CLEAN(text)), CLEAN(SUBSTITUTE())
Multiply by 1
but only the cell with the green-mark changes to a number, the rest stays the same. I browsed the internet but didn't get a solution either.
Edit:
I uploaded what is need to test the case on the drive. you can find it here
Help Appreciated
For your digit strings that you can't convert to text, from the comments it seems there are extra characters in that string not removable by TRIM or CLEAN.
Determine what those character are
Assume a "non-convertible" digit string is in A1
Enter the following formula
B1: =MID($A$1,ROWS($1:1),1) and fill down
C1: = UNICODE(B1) and fill down
From this you can determine the character to use in a SUBSTITUTE function.
For example:
From the above we see that the character code that we need to get rid of is 160.
So we use:
=SUBSTITUTE(A1,CHAR(160),"")
or, to convert it in one step to a number:
=--SUBSTITUTE(A1,CHAR(160),"")
Note If the character code is >255, use UNICHAR instead of CHAR in the SUBSTITUTE function.
Without an example, I use value() to convert what excel takes as text like so:
=value(left(“10kg”,2))
Or the following also works:
=left(“10kg”,2)*1
Note those double quotes should be the straight ones - sorry smartphone is not always smart...
And if leading or trailing spaces are an issue, then trim() is one solution.

How can I get the minimum value from a comma separated cell containing number

I found this formula which I can get the max value form a cell containing comma separated numbers. How can I get the min value using a similar approach?
=MATCH(1000,INDEX(FIND(","&ROW(INDIRECT("1:999"))&",",","&D2&","),0))
Cell content '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15'
You can try this:
=MIN(0+MID(SUBSTITUTE(A1, ",", REPT(" ",255)), 255*(ROW(INDIRECT("1:"&(1+LEN(A1)-LEN(SUBSTITUTE(A1,",","")))))-1)+1,255))
If one has FILTERXML:
=MIN(FILTERXML("<a><b>"&SUBSTITUTE(A1,",","</b><b>")&"</b></a>","//b"))
For the MAX, just replace the MIN with MAX.
I'd also go with FILTERXML. But if your string always follows this pattern it seems like the smallest number is always found on the left before the first comma which you could make perfect use of!
=--LEFT(A1,FIND(",",A1&",")-1)

Extract Sub-String from String in Excel (No VBA)

I have a series of paths in excel which follow the pattern:
C:\Folder\Subfolder1\SURNAME, Firstname\Subfolder2\SURNAME, Firstname - YYYY MM DD - Invoice.pdf
I cannot use VBA, so using an array formula, how would I extract SURNAME, Firstname?
You may use:
=TRIM(MID(SUBSTITUTE(A1,"\",REPT(" ",LEN(A1))),3*LEN(A1)+1,LEN(A1)))
Where 3* could be read as n-1, so change to whichever number to get the nth substring from a delimited string.
Another option, with access to FILTERXML:
=FILTERXML("<t><s>"&SUBSTITUTE(A1,"\","</s><s>")&"</s></t>","//s[position()=4]")
This would essentially pull the 4th substring from a "\" delimited string. Change position()=4 to the nth position if you like to retrieve other substrings. This option seems a bit longer, but could become handy when you want to retrieve multiple substrings where you just need to change up the XPATH.
After your commend, I think you might want to try:
=FILTERXML("<t><s>"&SUBSTITUTE(SUBSTITUTE(A6," - ","\"),"\","</s><s>")&"</s></t>","//s[position()=last()-2]")
With data in A1, in B1 enter:
=LEFT(RIGHT(A1,LEN(A1)-FIND("#",SUBSTITUTE(A1,"\","#",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))),1)),FIND("-",RIGHT(A1,LEN(A1)-FIND("#",SUBSTITUTE(A1,"\","#",LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))),1)))-2)
Or try,
In B1 copied across right until blank and all copied down :
=TRIM(MID(SUBSTITUTE("\"&MID($A1,FIND("\",$A1,4)+1,FIND("-",$A1,4)-FIND("\",$A1,4)-2),"\",REPT(" ",199)),COLUMN(A1)*399,199))

Formula to extract numbers from a text string

How could I extract only the numbers from a text string in Excel or Google Sheets? For example:
A1 - a1b23eg67
A2 - 15dgrgr156
Result desired is
B1 - 12367
B2 - 15156
You can do it with capture groups in Google Sheets
=REGEXREPLACE(A1,ʺ(\d)|.ʺ,ʺ$1ʺ)
Anything which matches the contents of the brackets (a digit) will be copied to the output, anything else replaced by an empty string.
Please see #Max Makhrov's answer to this question
or
=regexreplace(A1,ʺ[^\d]ʺ,ʺʺ)
to remove anything which isn't a digit.
Because you asked for Excel also,
If you have a subscription to office 365 Excel then you can use this array formula:
=--TEXTJOIN("",TRUE,IF(ISNUMBER(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)),MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),""))
Being an array formula it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
I would imagine there is a way to pull this off with =RegexExtract but I can't figure out how to get it to repeat the search after the first hit. Often with these regex function implementations there is a third parameter to repeat, but it doesn't look like google implemented it.
At any rate, the following formula will do the trick. It's just a little roundabout:
=concatenate(SPLIT( LOWER(A1) , "abcdefghijklmnopqrstuvwxyz" ))
This is converting the string to lower case, then splitting the string using any letter of the alphabet. This will return an array of the numbers left over, which we concatenate back together.
Update, switched over to =REGEXREPLACE() instead of extract...:
=regexreplace(A1, "[a-z]", "")
That's a much cleaner and obvious way of doing it than that concat(split()) nonsense.

Find the last number in a text string

I want to find the position of the last numeric character in a text string. I'm using this formula to do so:
MAX(IF(ISERROR(FIND({1;2;3;4;5;6;7;8;9;0},A1)),"",FIND({1;2;3;4;5;6;7;8;9;0},A1))
However, this doesn't work if the string contains repeating numbers.
For instance, when the string is "10ABC2010ABC" it will return 6 instead of 9.
When the string is "10ABC2131ABN" it does return 8 instead of 9.
Any ideas what's going on?
Here is working formula:
=MAX(IF(ISNUMBER(VALUE(MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1))),ROW(INDIRECT("1:" & LEN(A1)))))
press CTRL+SHIFT+ENTER to evaluate it.
Explanation:
ROW(INDIRECT("1:" & LEN(A1))) returns you array {1,2,3,...,Len(A1)}
using this array we can take each character in A1 cell: MID(A1,ROW(INDIRECT("1:" & LEN(A1))),1)
using VALUE(...) we tries to convert each character to number. It returns #VALUE! error for all characters exept 1,2,3,4,5,6,7,8,9,0
using ISNUMBER(...) we check whether VALUE(..) returns number or error, and if it returns number, we remember it's position.
final step - using Max(..) we find last position of numeric character
FIND only finds the position of the first instance of each number so it won't work for your requirements. Try using this formula
=MAX(IFERROR(FIND({1,2,3,4,5,6,7,8,9,0},A1,ROW(INDIRECT("1:"&LEN(A1)))),0))
confirmed with CTRL+SHIFT+ENTER
That also uses FIND but the ROW(INDIRECT part starts the search further along the string on each occasion. If there are no digits in A1 you get zero as the result (you could make that an error if you want)
Another possibility if you are using Excel 2010 or later is to use AGGREGATE function like this: [untested]
=AGGREGATE(14,6,FIND({1,2,3,4,5,6,7,8,9,0},A1,ROW(INDIRECT("1:"&LEN(A1)))),1)
That doesn't require "array entry"
See here for sample workbook with suggested formulas
And this array formula will also work... ok, ok... I know it uses offset :)
=1+LEN(A1)-MATCH(1;ISNUMBER(LEFT(RIGHT(A1;ROW(OFFSET(A1;0;0;LEN(A1);1))))*1)*1;0)

Resources