Function cleannumber(messynumber As String) As String
Dim placeholder As String
Dim counter As Integer
For counter = 1 To Len(messynumber)
If Mid(messynumber, counter, 1) Like "#" Then
placeholder = placeholder + Mid(messynumber, counter, 1)
End If
Next counter
cleannumber = placeholder
End Function
OK...Here's what to do
• Select the workbook you want to contain the program
• ALT+F11...to open the VBA editor
• Insert.Module...to include a new general module in that workbook
• Tools.References
...Find one of the Microsoft VBScript Regular Expressions resources and check it, then click: OK
(I used: Microsoft VBScript Regular Expressions 5.5)
• Copy the below code and paste it into the new module:
Code:Public Function PullOnly(strSrc As String, CharType As String) Dim RE As RegExp Dim regexpPattern As String Set RE = New RegExp CharType = LCase(CharType) Select Case CharType Case Is = "digits": regexpPattern = "\D" Case Is = "letters": regexpPattern = "\d" Case Else: regexpPattern = "" End Select RE.Pattern = regexpPattern RE.Global = True PullOnly = RE.Replace(strSrc, "") End Function Sub LeaveNumbers() Dim cCell As Range For Each cCell In Selection If cCell <> "" Then cCell.Value = "'" & PullOnly(cCell.Text, "digits") End If Next cCell End Sub
• Select the data range to be impacted in your worksheet
• ALT-F8...to see the list of available macros
• Select: LeaveNumbers
• Click: Run
Is that something you can work with?
This modification to the macro I posted previously will remove everything except digits and colons...
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 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
OK...Here's what to do
• Select the workbook you want to contain the program
• ALT+F11...to open the VBA editor
• Insert.Module...to include a new general module in that workbook
• Tools.References
...Find one of the Microsoft VBScript Regular Expressions resources and check it, then click: OK
(I used: Microsoft VBScript Regular Expressions 5.5)
• Copy the below code and paste it into the new module:
Code:Public Function PullOnly(strSrc As String, CharType As String) Dim RE As RegExp Dim regexpPattern As String Set RE = New RegExp CharType = LCase(CharType) Select Case CharType Case Is = "digits": regexpPattern = "\D" Case Is = "letters": regexpPattern = "\d" Case Else: regexpPattern = "" End Select RE.Pattern = regexpPattern RE.Global = True PullOnly = RE.Replace(strSrc, "") End Function Sub LeaveNumbers() Dim cCell As Range For Each cCell In Selection If cCell <> "" Then cCell.Value = "'" & PullOnly(cCell.Text, "digits") End If Next cCell End Sub
• Select the data range to be impacted in your worksheet
• ALT-F8...to see the list of available macros
• Select: LeaveNumbers
• Click: Run
Is that something you can work with?
WORKED LIKE A CHARM!!! came across this, desperately needed a solution. THANK YOU for building this. many thanks
excel 2007
We have plenty of ideas, but I think you need to tell us what your mixed text/number cells look like and what you want after the code runs first. The routines earlier in the thread removed all the text leaving only numbers with no delimiters, so something like this...I have numbers , text and minus signs in the cells and want to remove the text, but not the minus sign before the numbers. The above macro removes the minus signs unfortunately. Any ideas? Thanks
If that is what your data looks like, you do not need a macro... simply select the column or columns with that data, press CTRL+H to bring up Excel's Replace dialog box, type a space followed by an asterisk in the "Find what" field, leave the "Replace with" field empty, click the "Options>>" button and make sure the checkbox labeled "Match entire cell contents" is not checked, then click the "Replace All" button.The macro removes the letters GBP but also removes the - sign before the numbers, which I would like to remain.
[TABLE="width: 54"]
<colgroup><col width="72" style="width: 54pt; mso-width-source: userset; mso-width-alt: 2633;"> <tbody>[TR]
[TD="class: xl24, width: 72, bgcolor: transparent"]-4.20 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]10.12 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]11.98 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]0.93 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]9.42 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]11.86 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]3.70 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]11.86 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]14.78 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]9.76 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]-8.66 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]10.10 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]-3.56 GBP[/TD]
[/TR]
[TR]
[TD="class: xl24, bgcolor: transparent"]5.55 GBP[/TD]
[/TR]
</tbody>[/TABLE]