Function CleanCell(s As String) As String
Dim aRemove()
Dim i As Integer, x As Integer
Dim sClean As String
Dim lNumber As Long
'Parse out number
lNumber = GetNumber(s)
'Search for and remove any repeated instances of number
sClean = RemoveRepeatedNos(s, CStr(lNumber))
'First create array of all string values to be removed
aRemove = Array("-", "~", "*", "Undefined") ' In this array enter all string characters you want removing
'Now remove all unwanted characters
For i = 0 To UBound(aRemove)
sClean = Replace(sClean, aRemove(i), "")
Next i
'Return cleaned string to cell
CleanCell = sClean
End Function
Private Function RemoveRepeatedNos(s As String, sNumber As String) As String
Dim iFirst As Integer
Dim s1 As String
iFirst = InStr(1, s, sNumber)
s1 = Replace(s, sNumber, "")
If iFirst = 1 Then
s1 = sNumber & s1
Else
s1 = Left(s1, iFirst - 1) & sNumber & Right(s1, Len(s1) - iFirst + 1)
End If
RemoveRepeatedNos = s1
End Function
Private Function GetNumber(s As String) As Long
Dim i As Integer, x As Integer
For i = 1 To Len(s)
If IsNumeric(Mid(s, i, 1)) Then
x = i + 1
Do Until IsNumeric(Mid(s, x, 1)) = False
x = x + 1
Loop
GetNumber = Mid(s, i, x - i)
Exit Function
End If
Next i
End Function
Sub CleanSheet()
Dim c As Range
'Assuming data starts in row2:
For Each c In Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
c.NumberFormat = "@" 'Set cell as text to stop any leading zeros being removed
If Len(c) > 0 Then
c = CleanCell(c.Text)
End If
Next c
End Sub