Remove Non_Numeric from string

JonRowland

Active Member
Joined
May 9, 2003
Messages
417
Office Version
  1. 365
Platform
  1. Windows
Hi,

The following code will strip numbers from a string however I would like the opposite and like to strip all but numbers from a string which is then placed into a variable (in this case celltext.

Not IsNumeric doesn't work and IsText doesn't seem to work. My question is is there an opposite of IsNumeric?

VBA Code:
        While IsNumeric(Right(celltext, 1))
            celltext = Left(celltext, Len(celltext) - 1)
        Wend
        'Write the new text string back into the cell
        Selection.Value = celltext

Thanks Jon
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this...
VBA Code:
For X = 1 To Len(celltext)
  If Mid(celltext, X, 1) Like "[!0-9]" Then Mid(celltext, X) = " "
Next
Selection.Value = Application.Trim(celltext)
 
Upvote 0
If you wanted to replace them all at once:
VBA Code:
Dim RX As Object

Set RX = CreateObject("VBScript.RegExp")
RX.Global = True
RX.Pattern = "\D"
celltext = RX.Replace(celltext, "")
 
Upvote 0
Solution
Another one-pass way

VBA Code:
celltext = Evaluate(Replace("concat(if(isnumber(mid(#,sequence(len(#)),1)+0),mid(#,sequence(len(#)),1),""""))", "#", """" & celltext & """"))
 
Upvote 0
Another one-pass way

VBA Code:
celltext = Evaluate(Replace("concat(if(isnumber(mid(#,sequence(len(#)),1)+0),mid(#,sequence(len(#)),1),""""))", "#", """" & celltext & """"))
For those of you who do not have a version of Excel with the CONCAT or SEQUENCE functions in them, you can use this modified version of Peter's code line to do the same thing...
VBA Code:
celltext = Join(Evaluate(Replace("TRANSPOSE(IF(ISNUMBER(MID(""#"",ROW(1:99),1)+0),MID(""#"",ROW(1:99),1),""""))", "#", celltext)), "")
NOTE: I have assumed the text in the variable 'celltext' will not be longer than 99 characters in length.
 
Upvote 0
Rick and Peter, thanks guys you've given me more than one solution which is great. I think as I want to do the replace all at once Peter' code works for me. How
VBA Code:
VBA Code:
Dim RX As Object

Set RX = CreateObject("VBScript.RegExp")
RX.Global = True
RX.Pattern = "\D"
celltext = RX.Replace(celltext, "")

Thanks
Jon
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,137
Members
449,361
Latest member
VBquery757

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top