Need help stripping characters from beginning of string

kcleere

Active Member
Joined
Oct 4, 2006
Messages
314
Greetings,

I need some VBA code that will strip all characters including and prior to the first space...after a dash in a string. This data will always be in column B.

Example:

18 - John Doe

Would return John Doe.

I have tried but I have not yet nailed it so in seeking help from the brain trust here.

Thank you in advance.

Ken
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try the SPLIT function..

Something like
Code:
MyStr = "18 - John Doe"
MyVals = Split(MyStr, "- ")
MsgBox MyVals(1)
 
Upvote 0
I'm not familiar enough with VBA to understand how this will only act on col B. Please advise?
 
Upvote 0
Try

Code:
Sub Strip_Beginning()
Dim lr As Long, i As Long
lr = Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To lr
    If Instr(1,Cells(i, "B").Value, " -") <> 0 Then
        Cells(i, "B").Value = Split(Cells(i, "B").Value, "- ")(1)
    End If
Next i
End Sub
 
Last edited:
Upvote 0
It worked perfectly...thank you. I may be back for more..
Just so you know, you can do what you asked for manually just as easily. Assuming cell B1 does not contain a dash followed by a space, select Column B (otherwise, select from cell B2 on down), press CTRL+H to bring up Excel's Replace dialog box, put "*- " without the quote marks (that is an asterisk followed by a dash followed by a space character) in the "Find what" field, leave the "Replace with" field empty, click the "Options>>" button and make sure the "Match entire cell contents" checkbox is not checked, then click the "Replace All" button.

If, however, you want to still do it with a macro, this compact one will work as well (it just codifies what I described above)...
Code:
Sub StripFromBeginningToDashSpace()
  Range("B2", Cells(Rows.Count, "B").End(xlUp)).Replace "*- ", "", xlPart
End Sub
 
Upvote 0
Thank you Rick....the manual method will work yes but I'm giving this to a user and I need them to click as little as possible.
 
Upvote 0

Forum statistics

Threads
1,203,464
Messages
6,055,577
Members
444,799
Latest member
CraigCrowhurst

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