Remove numbers from dynamic range

THEEND

New Member
Joined
Mar 17, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi

I am working on a solution to remove any stray numbers that may appear in text in a selected range: Range("N3:Q" & lastRow).Select.

I have found a partial solution on here by Rick Rothstein (see below) but it is only for one column and, with my rather limited VBA skills, I haven't been able to adapt it to work with my selection.

Any help would be greatly appreciated.

Many thanks

TE

VBA Code:
Sub RemoveNumbersFromColumnB()
  Dim X As Long, R As Long, Data As Variant
  Data = Range("B1", Cells(Rows.Count, "B").End(xlUp))
  For R = 1 To UBound(Data)
    For X = 1 To Len(Data(R, 1))
      If Mid(Data(R, 1), X, 1) Like "[0-9]" Then Mid(Data(R, 1), X) = Chr(1)
    Next
    Data(R, 1) = Application.Trim(Replace(Data(R, 1), Chr(1), ""))
  Next
  Range("B1").Resize(UBound(Data)) = Data
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Which column would faithfully represent the last row with data? You're using B but looking over another range. If B has fewer rows than N, O, P, or Q those rows would not be included. Some things about that code could be changed I think. Why not just remove the number rather than change it to chr(1)? Gotta run for now though.
 
Upvote 0
Which column would faithfully represent the last row with data? You're using B but looking over another range. If B has fewer rows than N, O, P, or Q those rows would not be included. Some things about that code could be changed I think. Why not just remove the number rather than change it to chr(1)? Gotta run for now though.
Thanks Micron - yes, column B is best to determine the range as all cells will always be populated.

Regards,

TE
 
Upvote 0
Here is a way to do it with REGEX and REPLACE:
VBA Code:
Sub RemoveNumbers()
    Dim rng As Range, var As Variant, x As Long, y As Long
    
    ' currently looking at range A:H, the end row is defined using column B
    Set rng = Range("A1:H" & Range("B" & Rows.Count).End(xlUp).Row)
    var = rng.Value

    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[0-9]"
        For x = 1 To UBound(var)
            For y = 1 To UBound(var, 2)
                var(x, y) = .Replace(var(x, y), "")
            Next y
        Next x
    End With
    rng = var
End Sub
 
Upvote 0
Solution
Thanks Georgiboy, that worked perfectly!

Very much appreciated!

Thanks again,

TE
 
Upvote 0

Forum statistics

Threads
1,215,111
Messages
6,123,155
Members
449,098
Latest member
Doanvanhieu

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