Help with separating data in VBA

williamsabbie

New Member
Joined
Feb 27, 2015
Messages
3
Hi Guys & Girls,

I know there are a number of ways of splitting names by space, but i want to ignore the 2nd space.

So if the name is John Fred Bloggs.
I want it to be
John
Fred Bloggs

<tbody>
</tbody>


I have 2 macros built- one is a text to columns and the other is below. But both split into 3 or 4, rather than just 2 like i want.

Sub Split_Space()


Dim cell As Range
Dim cell1 As Range
Dim i As Long
Dim J As Long
Dim K As Long
Dim lngFindCol As Long
Dim lngFindCol1 As Long
Dim VarSplitString As Variant
Dim VarSplitString1 As Variant
Dim strContainer As String
Dim strContainer1 As String

i = 1
For Each cell In Range("c1:c" & Sheet1.Range("c" & Rows.Count).End(xlUp).Row)
VarSplitString = Split(cell.Value, " ")
For J = 1 To UBound(VarSplitString)
Sheet1.Cells(i, J + 4) = VarSplitString(J)
Next
i = i + 1
Next cell
i = 1
For Each cell In Range("c1:c" & Sheet1.Range("c" & Rows.Count).End(xlUp).Row)
VarSplitString = Split(cell.Value, " ")
For J = 1 To UBound(VarSplitString)
Sheet1.Cells(i, J + 3) = VarSplitString(K)
Next
i = i + 1
Next cell


End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the Board!

I know there are a number of ways of splitting names by space, but i want to ignore the 2nd space.
Try this:
Code:
Sub Split_Space()

Dim cell As Range
Dim VarSplitString As Variant

For Each cell In Range("c1:c" & Sheet1.Range("c" & Rows.Count).End(xlUp).Row)
    VarSplitString = InStr(cell, " ")
    cell.Offset(0, 1) = Left(cell, VarSplitString - 1)
    cell.Offset(0, 2) = Mid(cell, VarSplitString + 1, Len(cell))
Next cell

End Sub
 
Upvote 0
Thank you Joe,

That code worked fine, but if it came to a cell that only had a first name, it stopped.
My colleague helped me, and we came up with:

Sub Split_Space3()


Dim cell As Range
Dim i As Long
Dim J As Long
Dim K As Long
Dim VarSplitString As Variant
Dim strContainer As String
Dim strContainer1 As String

K = 1
For Each cell In Range("c1:c" & Sheet1.Range("c" & Rows.Count).End(xlUp).Row)
VarSplitString = Split(cell.Value, " ")
J = UBound(VarSplitString)
strContainer = VarSplitString(0)
If J > 0 Then strContainer1 = VarSplitString(1)
If J > 1 Then
For i = 2 To J
strContainer1 = strContainer1 & " " & VarSplitString(i)
Next i
End If

Sheet1.Range("D" & K) = strContainer
Sheet1.Range("E" & K) = strContainer1
K = K + 1
Next cell


End Sub
 
Upvote 0
That code worked fine, but if it came to a cell that only had a first name, it stopped.
You didn't mention that was a possibility, or else I would have programmed for that.

But glad you guys were able to figure it out.
 
Upvote 0
You didn't mention that was a possibility, or else I would have programmed for that.

But glad you guys were able to figure it out.



I didn't realise till I ran the code & went to the bottom of the data set and saw the blanks. It was an 'Oh, it also contains unknown. Doh'
 
Upvote 0
No worries. Sounds like it gave you guys a chance to break down the code a little bit and figure out how to alter it. That's not a bad thing!
 
Upvote 0

Forum statistics

Threads
1,215,559
Messages
6,125,517
Members
449,236
Latest member
Afua

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