VBA Split Function

heimdaloz

New Member
Joined
Feb 1, 2018
Messages
10
Hi,

I have a text value in a variable that contains a name. The name is in the format First, Middle, Last, however, Middle is optional.

I have used a split function to break the names in their individual components are output into columns.

If the name contains First, Middle, Last, I output the values into separate columns using Split.

However, if the name only contains First and Last, I want the Last to be recorded in the last column instead of the middle column.

For example[TABLE="width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]First[/TD]
[TD]Middle[/TD]
[TD]Last[/TD]
[/TR]
[TR]
[TD]John Mark Smith[/TD]
[TD]John[/TD]
[TD]Mark[/TD]
[TD]Smith[/TD]
[/TR]
[TR]
[TD]Hugo Harris[/TD]
[TD]Hugo[/TD]
[TD]Harris[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I want
[TABLE="width: 500"]
<tbody>[TR]
[TD]Name[/TD]
[TD]First[/TD]
[TD]Middle[/TD]
[TD]Last[/TD]
[/TR]
[TR]
[TD]John Mark Smith[/TD]
[TD]John[/TD]
[TD]Mark[/TD]
[TD]Smith[/TD]
[/TR]
[TR]
[TD]Hugo Harris[/TD]
[TD]Hugo[/TD]
[TD][/TD]
[TD]Harris[/TD]
[/TR]
</tbody>[/TABLE]


Any suggestions appreciated.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to the Board!

Try this (assuming data start in cell A2, and you want the names in columns B, C, and D):
Code:
Sub MySplit()

    Dim lrow As Long
    Dim r As Long
    Dim myArray() As String
    Dim mx As Long
    
    Application.ScreenUpdating = False
    
'   Find last row in column A with data
    lrow = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows, starting on row 2
    For r = 2 To lrow
'       Split entry in column A on the space
        myArray = Split(Cells(r, "A"), " ")
'       See how many items there are
        mx = UBound(myArray)
'       Split for two words
        If mx = 1 Then
            Cells(r, "B") = myArray(0)
            Cells(r, "D") = myArray(1)
        Else
'       Split for three words
            Cells(r, "B") = myArray(0)
            Cells(r, "C") = myArray(1)
            Cells(r, "D") = myArray(2)
        End If
'       Reinitialize array for next run
        Erase myArray
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Thanks, Joe4,

I need to split a string from an array, your code wasn't exactly what I needed, but I was able to use your code to write what I needed.

Thank you very much
 
Upvote 0
Here is another macro that you can consider (and modify for your array situation)...
Code:
[table="width: 500"]
[tr]
	[td]Sub SplitNames()
  Dim R As Long, Nam As String
  For R = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    Nam = Cells(R, "A").Value
    If Not Nam Like "* * *" Then Nam = Replace(Nam, " ", Space(2))
    Cells(R, "B").Resize(, 3) = Split(Nam)
  Next
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
I need to split a string from an array, your code wasn't exactly what I needed, but I was able to use your code to write what I needed.
I am curious. Can you provide an example where it wouldn't work? It worked on the examples you provided.
Can you also post the changes you made to the code?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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