Macro to delete all text from cells and leave numbers

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Here is another macro for you to try (change the two Const, short for constant, statements to match your actual data setup)...
Code:
Sub RemoveNonDigits()
  Dim X As Long, Z As Long, LastRow As Long, CellVal As String
  Const StartRow As Long = 1
  Const DataColumn As String = "A"
  Application.ScreenUpdating = False
  LastRow = Cells(Rows.Count, DataColumn).End(xlUp).Row
  For X = StartRow To LastRow
    CellVal = Cells(X, DataColumn)
    For Z = 1 To Len(CellVal)
      If Mid(CellVal, Z, 1) Like "[!0-9]" Then Mid(CellVal, Z, 1) = " "
    Next
    With Cells(X, DataColumn)
      .NumberFormat = "@"
      .Value = Replace(CellVal, " ", "")
    End With
  Next
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
You're very welcome..

Since you know exactly what you want to do (strip all non-numeric characters from cells), perhaps you could just use this abbreviated macro:

Code:
Sub LeaveNumbers()
Dim cCell As Range
Dim RE As RegExp
 
Set RE = New RegExp
RE.Pattern = "\D"
RE.Global = True
 
For Each cCell In Selection
    If cCell <> "" Then
        cCell.Value = "'" & RE.Replace(cCell.Text, "")
    End If
Next cCell
End Sub
Note: you'll still need the reference to the Microsoft VBScript Regular Expressions resource

I hope that helps.
 
Upvote 0
This one uses a helper cell, but it's still worth a look. Also, the flexibility to reverse the logic: i.e., only numbers or only text.

Code:
Function TextNum(ByVal txt As String, ByVal ref As Boolean) As String
' jindon
' http://www.mrexcel.com/forum/showthread.php?t=362461
' =TextNum(A1,1)
' 1 for Text only
' 0 for Numbers only
    With CreateObject("VBScript.RegExp")
        .Pattern = IIf(ref = True, "\d+", "\D+")
        .Global = True
        TextNum = .Replace(txt, "")
    End With
End Function
 
Upvote 0
This one uses a helper cell, but it's still worth a look. Also, the flexibility to reverse the logic: i.e., only numbers or only text.

Code:
Function TextNum(ByVal txt As String, ByVal ref As Boolean) As String
' jindon
' http://www.mrexcel.com/forum/showthread.php?t=362461
' =TextNum(A1,1)
' 1 for Text only
' 0 for Numbers only
    With CreateObject("VBScript.RegExp")
        .Pattern = IIf(ref = True, "\d+", "\D+")
        .Global = True
        TextNum = .Replace(txt, "")
    End With
End Function

If a UDF appeals to the OP, then here is the non-RegEx version of your function for him to consider...
Code:
Function TextNum(ByVal Txt As String, Optional Ref As Boolean = False) As String
  ' Ref argument... 1 for Text only, 0 for Numbers only
  Dim X As Long, CellVal As String
  For X = 1 To Len(Txt)
    If Mid(Txt, X, 1) Like "[" & Left("!", 1 - Ref) & "0-9]" Then Mid(Txt, X, 1) = " "
  Next
  TextNum = Replace(Txt, " ", "")
End Function
 
Upvote 0
Hi Rick,

Can you explain what exactly a non-RegEx version means?

Maybe I should have written it as non-RegExp? I was just referring to the fact that the function I posted uses native VB functions only and does not rely on a Regular Expression engine... the CreateObject("VBScript.RegExp") in your function... to perform its action.
 
Upvote 0
If a UDF appeals to the OP, then here is the non-RegEx version of your function for him to consider...
Rich (BB code):
Function TextNum(ByVal Txt As String, Optional Ref As Boolean = False) As String
  ' Ref argument... 1 for Text only, 0 for Numbers only
  Dim X As Long, CellVal As String
  For X = 1 To Len(Txt)
    If Mid(Txt, X, 1) Like "[" & Left("!", 1 - Ref) & "0-9]" Then Mid(Txt, X, 1) = " "
  Next
  TextNum = Replace(Txt, " ", "")
End Function

Hi Rick

I get the same result for Ref = True/False. Maybe you meant:

Code:
 ... & Left("!", 1 [B][COLOR=red]+[/COLOR][/B] Ref) & ...

or

Code:
 ... & IIf(Ref, "", "!") & ...
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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