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
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
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
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
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
here is the non-RegEx version
Hi Rick,
Can you explain what exactly a non-RegEx version means?
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
... & Left("!", 1 [B][COLOR=red]+[/COLOR][/B] Ref) & ...
... & IIf(Ref, "", "!") & ...