Remove numbers from end of string

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I would like to remove all of the trailing numbers, but can it be done in the same column. I've seen some ways to do it in another column, but how about the same column.

BeforeAfter
House Gifts 001House Gifts
House Gifts 002House Gifts
House Gifts 003House Gifts
House Gifts 004House Gifts
Car Wash 35Car Wash
Car Wash 36Car Wash
Car Wash 37Car Wash
Hand Washing Kit 12Hand Washing Kit
Hand Washing Kit 13Hand Washing Kit
Hand Washing Kit 14Hand Washing Kit
Hand Washing Kit 1Hand Washing Kit
Hand Washing Kit 2Hand Washing Kit
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
To do it in the same column will most likely require VBA.
Are you open to a VBA solution?
 
Upvote 0
How about:

Code:
Sub RemoveNum()
Dim lr, i, p As Long, txt As String

lr = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lr
 txt = Cells(i, "A") ' text string in cell
 For p = 0 To 9
   txt = Replace(txt, p, "")
 Next p
 Cells(i, "A") = Trim(txt)
Next i
  
End Sub
 
Upvote 1
Try this, which only requires one loop (you want to limit loops as much as possible, as they are generally slow/inefficient):
VBA Code:
Sub MyFixData()

    Dim lr As Long
    Dim r As Long
    Dim n As Long
   
    Application.ScreenUpdating = False
   
'   Find last row in column A with data
    lr = Cells(Rows.Count, "A").End(xlUp).Row
   
'   Loop through all rows
    For r = lr To 1 Step -1
'       Find location of last space in entry
        n = InStrRev(Trim(Cells(r, "A")), " ")
'       Extract last number from string
        If n > 1 Then Cells(r, "A") = Left(Cells(r, "A"), n - 1)
    Next r
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 1
With out loop.
Try this:

VBA Code:
Sub deletenumbers()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("=IF({1},LEFT(" & .Address & ",LEN(" & .Address & ")-LEN(TRIM(RIGHT(SUBSTITUTE(" & .Address & ", "" "",REPT("" "",99)),99)))))")
  End With
End Sub
 
Upvote 0
With out loop.
Try this:

VBA Code:
Sub deletenumbers()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("=IF({1},LEFT(" & .Address & ",LEN(" & .Address & ")-LEN(TRIM(RIGHT(SUBSTITUTE(" & .Address & ", "" "",REPT("" "",99)),99)))))")
  End With
End Sub
Nice Dante!

I figured that there had to be a way without loops, but couldn't come up with it.
I haven't used "EVALUATE" that much.
 
Upvote 0
Thanks Joe for your comments.

In some versions of excel the first IF is not necessary. But in my version of excel I always have to put it.

VBA Code:
.Value = Evaluate("=LEFT(" & .Address & ",LEN(" & .Address & ")-LEN(TRIM(RIGHT(SUBSTITUTE(" & .Address & ", "" "",REPT("" "",99)),99))))")


It can also be simplified to something like this, so as not to cut through .address every time it's required:

VBA Code:
Sub deletenumbers()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("=IF({1},LEFT(@,LEN(@)-LEN(TRIM(RIGHT(SUBSTITUTE(@, "" "",REPT("" "",99)),99)))))", "@", "" & .Address & ""))
  End With
End Sub

;)
 
Upvote 1
Solution
Thank you to all. These solutions worked great. I too like the Evaluate method, but it always confuses me.
 
Upvote 0

Forum statistics

Threads
1,215,139
Messages
6,123,264
Members
449,093
Latest member
Vincent Khandagale

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