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, "", "!") & ...
Function TextNum(ByVal Txt As String, Optional Ref As Boolean = False) As String
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
As for your IIf function suggestion... I try to never use that function unless I can't find a better alternative.
Hi, Rick
Just for the record, I avoid RegExp solutions almost as enthusiastically as I avoid array-formula solutions. They're usually unwieldy and all but impossible for typical users to maintain. However, in occaissional situations, like this thread's issue, RegExp is so much simpler than the LIKE operator approach that I had to yield.
If Mid(CellVal, Z, 1) Like "[!0-9]" Then Mid(CellVal, Z, 1) = " "
If InStr("0123456789", Mid(CellVal, Z, 1)) = 0 Then Mid(CellVal, Z, 1) = " "
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 InStr("0123456789", Mid(CellVal, Z, 1)) = 0 Then Mid(CellVal, Z, 1) = " "
Next
With Cells(X, DataColumn)
.NumberFormat = "@"
.Value = Replace(CellVal, " ", "")
End With
Next
Application.ScreenUpdating = True
End Sub
Function TextNum(ByVal Txt As String, Optional Ref As Boolean = False) As String
Dim X As Long, CellVal As String
For X = 1 To Len(Txt)
If InStr("0123456789", Mid(Txt, X, 1)) > 0 = Ref Then Mid(Txt, X, 1) = " "
Next
TextNum = Replace(Txt, " ", "")
End Function
Actually, yes...(if my opinion actually matters to anybody) that code is much more readable.
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 Not IsNumeric(Mid(CellVal, Z, 1)) Then Mid(CellVal, Z, 1) = " "
Next
With Cells(X, DataColumn)
.NumberFormat = "@"
.Value = Replace(CellVal, " ", "")
End With
Next
Application.ScreenUpdating = True
End Sub
Function TextNum(ByVal Txt As String, Optional Ref As Boolean = False) As String
Dim X As Long, CellVal As String
For X = 1 To Len(Txt)
If IsNumeric(Mid(Txt, X, 1)) = Ref Then Mid(Txt, X, 1) = " "
Next
TextNum = Replace(Txt, " ", "")
End Function
This modification to the macro I posted previously will remove everything except digits and colons...Would there be a way to alter that code slightly so that colons ( : ) would be left alone, too? I'm looking to remove all letters and punctuation (except for colons).
Example of what I have:
00:00:03:23 00:00:05:23 The quick brown fox...
00:00:06:23 00:00:12:12 ...jumps over the lazy...
00:00:25:03 00:00:32:11 brown dog!
What I want the macro to do:
00:00:03:23 00:00:05:23
00:00:06:23 00:00:12:12
00:00:25:03 00:00:32:11
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 Not IsNumeric(Mid(CellVal, Z, 1)) And Mid(CellVal, Z, 1) <> ":" Then Mid(CellVal, Z, 1) = " "
Next
With Cells(X, DataColumn)
.NumberFormat = "@"
.Value = Replace(CellVal, " ", "")
End With
Next
Application.ScreenUpdating = True
End Sub