How would I get only the numbers from excel - excel

How would I extract all the numbers from a cell, so for example A1 cell contains: "AA59" I would like the formula to extract 59 and ignore AA. I can use =if(right(A1),2) formula but if A1 contains value AA5 then it will select last two characters (which is A5), so this is wrong?

Here is a native worksheet formula solution. It's a little inelegant compared to a VBA user-defined-function, but is a standard (non-array) formula and looks more complex than it actually is.
      
The formula in B21 is,
=MID(A1, MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))<48)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>57))*1E+99,,)), 99)
Fill down as necessary. Wrap the whole thing in a VALUE(...) function or precede MID with a double unary (aka double minus or --) to convert the text to a true number.

As an alternative, consider 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)
to extract digits from a mixed cell:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

If you are always trying to get a single or double digit number from the end of the string try this version:
=LOOKUP(100,RIGHT(A2,{1,2})+0)

try this macro
Function RakamCikart(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "[0-9]"
RakamCikart = .Replace(Txt, "")
End With
End Function

Related

Im trying to extract all the words in a single cell which contain following characters "ROWID" into other columns

For example, if i have a cell which contains
ADDR_ROWID,LOC_ROWID,ADDR_LINE_1,ADDR_LINE_2,CITY,ST_PROV,CNTRY_CODE,POSTAL_CODE,STAT_IND,ADDR_TYPE_ROWID in cell A1
i want to extract ADDR_ROWID in B1, LOC_ROWID in C1 and ADDR_TYPE_ROWID in D1 cells respectively. is there any way possibly doing it?
Try FILTERXML():
Formula in B1:
=TRANSPOSE(FILTERXML("<t><s>"&SUBSTITUTE(A1,",","</s><s>")&"</s></t>","//s[substring(., string-length(.)-5) = '_ROWID']"))
We basically created the missing ends-with() xpath function. See here for a more in-depth explaination why this works.
Or; with the newest versions of Excel you can even use:
=LET(X,TEXTSPLIT(A1,","),FILTER(X,RIGHT(X,6)="_ROWID",""))
Note: The latter is currently case-insensitive where the FILTERXML() option is case-sensitive.
So a method with find(), iferror() and if(), perhaps simpler than some:
Formula in cell B1:
IF(IFERROR(FIND("ADDR_ROWID",A1,1),0)>0,"ADDR_ROWID","")
The others just change the text in the find() & if().
If you have access to VBA/Macros, then this snip-it might help;
Sub caller_getROWIDs()
GetROWIDs [A1], [B1]
End Sub
Sub GetROWIDs(rInput As Range, rOutput As Range)
For Each SubString In Split(rInput.Value, ",")
If InStr(SubString, "ROWID") Then
rOutput.Value2 = SubString
Set rOutput = rOutput.Offset(, 1)
End If
Next
End Sub
You can achive this with just excel formulas.
first you need to extract all the words in the cell which is separated by comma ",".
FORMULA 1.
=TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",LEN(A1))),(SEQUENCE(,15)-SEQUENCE(,LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+1,1,1))*LEN(A1)+1,LEN(A1)))
Note: SEQUENCE(,15) is i am assuming there could be 15 word(I know there is 10 words since its only one cell imagine if you have more than 100), you can put any number as long as its not less then the actual word might be.
once you extract all the words from the cell then you need to filter your conditions which is if the letter contains "ROWID".
FORMULA 2.
=FILTER(TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",LEN(A1))),(SEQUENCE(,15)-SEQUENCE(,LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+1,1,1))*LEN(A1)+1,LEN(A1))),ISNUMBER(SEARCH("ROWID",TRIM(MID(SUBSTITUTE(A1,",",REPT(" ",LEN(A1))),(SEQUENCE(,15)-SEQUENCE(,LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))+1,1,1))*LEN(A1)+1,LEN(A1))))))
You can just use the formula 2.

Check if cell contains other characters than a-z; 0-9

I am searching for a way in excel to check a cell for other characters than the alphabet a-z, numbers 0-9 and the character "-"
In column "A" I have a list of product names like
A1: samsung-s7-black
A2: apple-phone-6-silver
A3: huawei-p9-limited-edition!
In column "B" I would like to get the following info
B1:
B2:
B3: !
Basically I am looking for a "negative" search in which i don't define which characters are not allowed but more which characters are allowed in my cell and output the characters that do not match. If this could be done without VBA even better.
If you have Office 365 / Excel 2016, you can use the TEXTJOIN function in an array formula:
B1: =TEXTJOIN("",TRUE,IF((CODE(MID(A3,seq,1))>=97)*(CODE(MID(A3,seq,1))<=122)+(CODE(MID(A3,seq,1))=45)+ISNUMBER(--MID(A3,seq,1))=1,"",MID(A3,seq,1)))
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
seq is a Name'd formula that refers to:
=ROW(INDEX(Sheet1!$1:$65535,1,1):INDEX(Sheet1!$1:$65535,LEN(INDIRECT("RC[-1]",FALSE)),1))
Note that we use the RC version of INDIRECT so the formula needs to be placed in the adjacent column of the string being tested.
Oh, and if you have mixed case in your actual data, replace A1 in the formula with =LOWER(A1)
=TEXTJOIN("",TRUE,IF((CODE(MID(LOWER(A1),seq,1))>=97)*(CODE(MID(LOWER(A1),seq,1))<=122)+(CODE(MID(LOWER(A1),seq,1))=45)+ISNUMBER(--MID(LOWER(A1),seq,1))=1,"",MID(LOWER(A1),seq,1)))
If you do not have the TEXTJOIN function, you could do a nested SUBSTITUTE or use a VBA solution.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a",""),"b",""),"c",""),"d",""),"e",""),"f",""),"g",""),"h",""),"i",""),"j",""),"k",""),"l",""),"m",""),"n",""),"o",""),"p",""),"q",""),"r",""),"s",""),"t",""),"u",""),"v",""),"w",""),"x",""),"y",""),"z",""),"0",""),"1",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9",""),"-","")
Here's a VBA solution. Put this in a workbook module, and you can call with =remove_alphanumeric(A1)
Function remove_alphanumeric(InputString As String) As String
Dim i As Integer, strLen As Integer
Dim tmp_str As String, final As String
final = ""
i = 1
strLen = Len(InputString)
For i = 1 To strLen
tmp_str = Mid(InputString, i, 1)
If InStr(1, "abcdefghijklmnopqrstuvwxyz0123456789-", tmp_str) = 0 Then final = final + tmp_str
Next
remove_alphanumeric = final
End Function

How to subtract one text cell from another in Excel?

I have two text cells in Excel as shown below which contains strings (A1 and A2), I am trying to remove A2 cell characters from A1 cell string and store the result in A3.
I have tried using few functions like SEARCH(), TEXT(), RIGHT() .. but couldnt achieve what I would need as result.
I could do this easily using any programming language (Python, C, etc.,) - just need a loop and couple of string functions. But couldn't figure out the way to get the same using Excel Formula.
Any thoughts how can we get this?
Doing this via a UDF would be pretty straightforward. Basically just loop through the ins character by character and use replace() to swap the character out with nothing;
Function textSubtract(startString As String, subtractString As String) As String
'Function to subtract characters in one string from another string
'Loop through every character in subtractString
Dim charCounter As Integer
For charCounter = 1 To Len(subtractString)
'Replace out the character in startString
startString = Replace(startString, Mid(subtractString, charCounter, 1), "")
Next charCounter
'Return
textSubtract = startString
End Function
Then you can call it like:
In B2 enter:
=MID($A$2,COLUMN()-1,1)
and copy across (this isolates the characters). In B3 enter:
=SUBSTITUTE($A$1,B2,"")
In C3 enter:
=SUBSTITUTE(B3,C2,"")
and copy across (this removes each character):

vector element operations in excel

I am trying to perform the following in one steps (one formula):
Strip a letter from a column of elements and add them up.
Example:
Data:
1x
2y
3x
I want to strip letters and add up numbers all in one formula.
I understand that I could have a helper column in which I strip letters x,y,z and then have a formula to add up the numbers, but I don't want to do this.
Thanks for any suggestions.
Assuming one entry per cell:
Is there only one letter at the end? If so, you can use:
=SUMPRODUCT(--LEFT(A1:A100,LEN(A1:A100)-1))
If there might be multiple letters at the end, a simple UDF would be simpler:
Option Explicit
Function AddStrings(rg As Range)
Dim L As Long
Dim I As Long
For I = 1 To rg.Count
L = L + Val(rg(I))
Next I
AddStrings = L
End Function
EDIT: If some of the cells might be blank, you can use either the UDF, or, if you prefer the formula, this array-entered formula:
=SUM(IFERROR(--LEFT(A1:A100,LEN(A1:A100)-1),0))
To array-enter a formula, after entering
the formula into the cell or formula bar, hold down
ctrl-shift while hitting enter. If you did this
correctly, Excel will place braces {...} around the formula.
Assuming that the format is consistent, you can do something like
=VALUE(LEFT(A1,1))+VALUE(MID(A1,4,1))+VALUE(MID(A1,7,1))
If the format is not consistent, things get more difficult. Let me know and I will expand the answer.
EDIT:
This function works with a variable length text, assuming that the fields are separated by the spaces and have one letter after the number:
Function AddValues(Text As String)
Dim Tokens() As String, I As Integer
Tokens = Split(Text)
For I = 0 To UBound(Tokens)
AddValues = AddValues + Val(Left(Tokens(I), Len(Tokens(I)) - 1))
Next I
End Function

Extract the last substring from a cell

I have names in a column. I need to split just the last names from that column into another column.
The last name is delimited by a space from the right side.
The contents in cell A2 = Alistair Stevens and I entered the formula in cell B2 (I need 'Stevens' in cell B2)
I tried using the following formulas:
=RIGHT(A2,FIND(" ",A2,1)-1)
=RIGHT(A2,FIND(" ",A2))
Both these formulas work for this cell but when I fill it down / copy and paste it for the cells below it doesn't work. I get the wrong values!!
A3 -> David Mckenzie
B3 -> Mckenzie
This works, even when there are middle names:
=MID(A2,FIND(CHAR(1),SUBSTITUTE(A2," ",CHAR(1),LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))+1,LEN(A2))
If you want everything BUT the last name, check out this answer.
If there are trailing spaces in your names, then you may want to remove them by replacing all instances of A2 by TRIM(A2) in the above formula.
Note that it is only by pure chance that your first formula =RIGHT(A2,FIND(" ",A2,1)-1) kind of works for Alistair Stevens. This is because "Alistair" and " Stevens" happen to contain the same number of characters (if you count the leading space in " Stevens").
The answer provided by #Jean provides a working but obscure solution (although it doesn't handle trailing spaces)
As an alternative consider a vba user defined function (UDF)
Function RightWord(r As Range) As Variant
Dim s As String
s = Trim(r.Value)
RightWord = Mid(s, InStrRev(s, " ") + 1)
End Function
Use in sheet as
=RightWord(A2)
Try this function in Excel:
Public Shared Function SPLITTEXT(Text As String, SplitAt As String, ReturnZeroBasedIndex As Integer) As String
Dim s() As String = Split(Text, SplitAt)
If ReturnZeroBasedIndex <= s.Count - 1 Then
Return s(ReturnZeroBasedIndex)
Else
Return ""
End If
End Function
You use it like this:
First Name (A1) | Last Name (A2)
Value in cell A1 = Michael Zomparelli
I want the last name in column A2.
=SPLITTEXT(A1, " ", 1)
The last param is the zero-based index you want to return. So if you split on the space char then index 0 = Michael and index 1 = Zomparelli
The above function is a .Net function, but can easily be converted to VBA.
If you want to get the second to last word in a text, you can use this macro as a function in your spreadsheet:
Public Function Get2ndText(S As String) As String
Dim sArr() As String
Dim i As Integer
sArr = Split(S, " ")
'get the next to the last string
i = UBound(sArr) - 1
Get2ndText = sArr(i)
End Function
Then in your spreadsheet B1 as the text:
CURRENT OWNER 915 BROADWAY ST HOUSTON TX 77012-2126
in B2 your formula would be:
=Get2ndText(B1)
The result would be
TX
Simpler would be:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",99)),99))
You can use A2 in place of TRIM(A2) if you are sure that your data doesn't contain any unwanted spaces.
Based on concept explained by Rick Rothstein:
http://www.excelfox.com/forum/showthread.php/333-Get-Field-from-Delimited-Text-String
Sorry for being necroposter!
Right(A1, Len(A1)-Find("(asterisk)",Substitute(A1, "(space)","(asterisk)",Len(A1)-Len(Substitute(A1,"(space)", "(no space)")))))
Try this. Hope it works.
Try this:
=RIGHT(TRIM(A2),LEN(TRIM(A2))-FIND(" ",TRIM(A2)))
I was able to copy/paste the formula and it worked fine.
Here is a list of Excel text functions (which worked in May 2011, and but is subject to being broken the next time Microsoft changes their website). :-(
You can use a multiple-stage-nested IF() functions to handle middle names or initials, titles, etc. if you expect them. Excel formulas do not support looping, so there are some limits to what you can do.
RIGHT return whatever number of characters in the second parameter from the right of the first parameter. So, you want the total length of your column A - subtract the index. which is therefore:
=RIGHT(A2, LEN(A2)-FIND(" ", A2, 1))
And you should consider using TRIM(A2) everywhere it appears...
Try this:
Right(RC[-1],Len(RC[-1])-InStrRev(RC[-1]," "))

Resources