Remove non-numbers in Column F

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello the following code I used in the past on another project. In this case I need to change it so it will start on Row 5 of Column F and the range will depend on the height of my table. I do not need a input box.

VBA Code:
Sub RemoveAllNonNums_Macro()
Dim myR As Range
Dim myRange As Range

Set myRange = Application.Selection
Set myRange = Application.InputBox("select one range that you want to remove non-numeric characters", RemoveAllNonNums, myRange.Address, Type:=8)
For Each myR In myRange
    myOut = ""
    For i = 1 To Len(myR.Value)
        tmp = Mid(myR.Value, i, 1)
        If tmp Like "[0-9]" Then
            myStr = tmp
        Else
            myStr = ""
        End If
        myOut = myOut & myStr
    Next i
    myR.Value = myOut
Next

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
In this case I need to change it so it will start on Row 5 of Column F

Try this:

VBA Code:
Sub RemoveAllNonNums_Macro()
  Dim c As Range
  
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9]"
    .Global = True
    For Each c In Range("F5", Range("F" & Rows.Count).End(3))
      c.Value = .Replace(c.Value, "")
    Next
  End With
End Sub
 
Upvote 0
Solution
If your data happens to be large, this would be a bit quicker due to not interacting with the worksheet each row.

VBA Code:
Sub KeepDigitsOnly()
  Dim a As Variant
  Dim i As Long
  
  With Range("F5", Range("F" & Rows.Count).End(xlUp))
    a = .Value
    With CreateObject("VBScript.RegExp")
      .Global = True
      .Pattern = "\D"
      For i = 1 To UBound(a)
        a(i, 1) = .Replace(a(i, 1), "")
      Next i
    End With
    .Value = a
  End With
End Sub
 
Upvote 0
If it were the case with lots of data, here is my proposal:

VBA Code:
Sub RemoveAllNonNums_Macro()
  Dim i As Long, a As Variant
  
  a = Range("F5", Range("F" & Rows.Count).End(3)).Value
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9]"
    .Global = True
    For i = 1 To UBound(a, 1)
      a(i, 1) = .Replace(a(i, 1), "")
    Next
  End With
  Range("F5").Resize(UBound(a, 1)).Value = a
End Sub
 
Upvote 0
If it were the case with lots of data, here is my proposal:
Is that any different to my proposal?
- read the data into an identical array
- use an identical reg ex pattern - non-digit (just written with different syntax)
- process the array row-by-row in an identical way
- put the results back in the identical range
 
Upvote 0
Try this:

VBA Code:
Sub RemoveAllNonNums_Macro()
  Dim c As Range
 
  With CreateObject("VBScript.RegExp")
    .Pattern = "[^0-9]"
    .Global = True
    For Each c In Range("F5", Range("F" & Rows.Count).End(3))
      c.Value = .Replace(c.Value, "")
    Next
  End With
End Sub
That was perfect! Thank you!
 
Upvote 0
If your data happens to be large, this would be a bit quicker due to not interacting with the worksheet each row.

VBA Code:
Sub KeepDigitsOnly()
  Dim a As Variant
  Dim i As Long
 
  With Range("F5", Range("F" & Rows.Count).End(xlUp))
    a = .Value
    With CreateObject("VBScript.RegExp")
      .Global = True
      .Pattern = "\D"
      For i = 1 To UBound(a)
        a(i, 1) = .Replace(a(i, 1), "")
      Next i
    End With
    .Value = a
  End With
End Sub
Thank you for reaching out. I feel so spoiled sometimes to have choices. Much appreciated.
 
Upvote 0
*** IGNORE - I misunderstood the question ***

Assuming your values are all constants (no formulas), what about this one-liner...
VBA Code:
Sub RemoveNonNumbers()
  Range("F5", Cells(Rows.Count, "F").End(xlUp)).SpecialCells(xlConstants, xlTextValues).ClearContents
End Sub
 
Last edited:
Upvote 0
Assuming your values are all constants (no formulas), what about this one-liner...
VBA Code:
Sub RemoveNonNumbers()
  Range("F5", Cells(Rows.Count, "F").End(xlUp)).SpecialCells(xlConstants, xlTextValues).ClearContents
End Sub
:unsure: OP seemed to be asked for the same operation as the original code, just the range determined in a different way?
 
Upvote 0
:unsure: OP seemed to be asked for the same operation as the original code, just the range determined in a different way?
I was just offering him what I thought is an interesting (and more compact) alternative in case he might like to make use of it.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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