Removing Extraneous Numbers w/ Specific Format

aje35

New Member
Joined
Jun 22, 2010
Messages
47
I have a column of names that I pass through a macro. The names get concatenated with an ID number in some cases. e.g. "Campbell" becomes "0131 Campbell" and if it passes through again it can become "4438 0131 Campbell". The number is always 4 digits and there is always a space. I was looking for some way to code in VBA an IF statement that would look for a #### followed by a space and if this formatting exists (or exists in multiple iterations) then it would remove those characters.

Any help would be appreciated.

Cheers,

AJ
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try something like this...

Code:
For Each cell In Range("A1", Range("A" & Rows.Count).End(xlUp))
    If cell.Value Like "#### *" Then cell.Value = Mid(cell.Value, 6)
Next cell
 
Upvote 0
That works perfectly in changning "0004 Goudon" to "Goudon"!

Is there a reason that the following wouldn't work for changing "0005 4885 Goudon" to "Goudon"? Also do you think there is any more optimal way to code this to decrease run time?

Code:
For Each cell In Range("A1", Range("A" & Rows.Count).End(xlUp))
    If cell.Value Like "#### *" Then cell.Value = Mid(cell.Value, 6)
    If cell.Value Like "#### #### *" Then cell.Value = Mid(cell.Value, 11)
Next cell

Thank you for your help.
 
Upvote 0
This will remove multiples e.g.; 1234 5678 9876 Campbell will become Campbell

Code:
Sub Macro1()

    Dim rng As Range, vdata As Variant, i As Long

    Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
    vdata = rng

    For i = LBound(vdata, 1) To UBound(vdata, 1)
        If vdata(i, 1) Like "#### *" Then vdata(i, 1) = Strip(vdata(i, 1))
    Next i
    
    rng = vdata

End Sub

Function Strip(ByVal str As String) As String
    str = Mid(str, 6)
    If str Like "#### *" Then str = Strip(str)
    Strip = str
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,122
Members
452,381
Latest member
Nova88

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