Macro to cut everything but first two words and paste to the column to the right

danknacity

New Member
Joined
Jun 18, 2014
Messages
5
Hi All,

New to Macros (Excel for Mac 2011), but am hoping someone can hep me out. I am looking to take a large and growing database of names followed by string of text all in column A and have them separated into two cells

I am currently dealing with the following (names have been changed for privacy)

Column A

A1: Trey Anastasio is cool
A2: Mike Gordon likes to eat ham.
A3: Paige McConnell is not very good at writing excel macros.

I would like to have the return look like:

Column A Column B
A1: Trey Anastasio B1: is cool
A2: Mike Gordon B2: likes to eat ham.
A3: Paige McConnell B3:is not very good at writing excel macros.

I have scoured the internet with no luck. Can someone please assist me?

Thank you in advance. I very much appreciate your help.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Col A
Col B
Col C
Trey Anastasio is cool
Trey Anastasiois cool
Mike Gordon likes to eat ham.Mike Gordonlikes to eat ham.
Paige McConnell is not very good at writing excel macros.Paige McConnell
is not very good at writing excel macros.

<tbody>
</tbody>

formula
B1 =LEFT(A1,FIND("|",SUBSTITUTE(A1," ","|",2)))
C1=MID(A1,FIND("|",SUBSTITUTE(A1," ","|",2))+1,99)
 
Upvote 0
Thanks Texasalynn, is there anyway to have this outcome via Macro, or is my best bet to just fill down?



Col ACol BCol C
Trey Anastasio is coolTrey Anastasiois cool
Mike Gordon likes to eat ham.Mike Gordonlikes to eat ham.
Paige McConnell is not very good at writing excel macros.Paige McConnellis not very good at writing excel macros.

<tbody>
</tbody>

formula
B1 =LEFT(A1,FIND("|",SUBSTITUTE(A1," ","|",2)))
C1=MID(A1,FIND("|",SUBSTITUTE(A1," ","|",2))+1,99)
 
Upvote 0
here you go

Code:
Sub Split_Cell()
Dim i As Long, LR As Long
Dim Part1 As String, Part2 As String

LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    Part1 = _
        Left(Cells(i, 1), WorksheetFunction.Find("|", WorksheetFunction.Substitute(Cells(i, 1), " ", "|", 2)) - 1)
    Part2 = _
        Mid(Cells(i, 1), WorksheetFunction.Find("|", WorksheetFunction.Substitute(Cells(i, 1), " ", "|", 2)) + 1, 99)
    Cells(i, 1) = Part1
    Cells(i, 2) = Part2
Next i
MsgBox "Done"
End Sub
 
Upvote 0
Thank you so much! Unfortunately I am getting a bug. It gets through most of a 20 row document but 19 and 20 don't work.

Error message: Run-time error '1004':
Method 'Find' of object 'WorksheetFunction' failed

Debug highlights the following lines:

Part1 = _
Left(Cells(i, 1), WorksheetFunction.Find("|", WorksheetFunction.Substitute(Cells(i, 1), " ", "|", 2)) - 1)|

Any suggestion how to fix it?


here you go

Code:
Sub Split_Cell()
Dim i As Long, LR As Long
Dim Part1 As String, Part2 As String

LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    Part1 = _
        Left(Cells(i, 1), WorksheetFunction.Find("|", WorksheetFunction.Substitute(Cells(i, 1), " ", "|", 2)) - 1)
    Part2 = _
        Mid(Cells(i, 1), WorksheetFunction.Find("|", WorksheetFunction.Substitute(Cells(i, 1), " ", "|", 2)) + 1, 99)
    Cells(i, 1) = Part1
    Cells(i, 2) = Part2
Next i
MsgBox "Done"
End Sub
 
Upvote 0
Running the Macro on:

Trey Anastasio is Ccool
Mike Gordon likes to eat Ham
Paige McConnell is not very good at writing macros

<tbody>
</tbody>

Returns the following

Trey Anastasio is cool
Mike Gordonlikes to eat ham
Paige McConnellis not very good at writing macros

<tbody>
</tbody>


In effort to fix the top one, I try running again and get the error from before.

I greatly appreciate all your help on this. Let me know if anything additional will help clarify

post the sample of those line
 
Upvote 0
so you posted that 19 and 20 aren't working, but your sample is the same as your originals. So I'm not sure what your issue is so I can't answer
 
Upvote 0
I did not want to use the actual data as it is confidential. I am having the issue I just described. First line isn't working. Went to run the Macro again when 1 line wasn't separated but the two below were and got the error message and debug info described earlier.

so you posted that 19 and 20 aren't working, but your sample is the same as your originals. So I'm not sure what your issue is so I can't answer
 
Upvote 0
so if your data starts on line one then the macro wasn't setup for that you would need to change the
For i = 1 to LR

I can't tell you any more without knowing more. I was interesting in seeing if perhaps there wasn't the same amount of spaces in the data.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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