Put each word of string in different cells

chuckchuckit

Well-known Member
Joined
Sep 18, 2010
Messages
541
How would I for instance populate the cells for a header using one word each per cell?
Code:
Dim MyString
MyString = "These are the header words to go one word in each cell of same row."
Does each word need to be comma delimited, or is there a loop that can easily pull them out per cell?

Couldn't find anything in my searches so thought I would ask here.

Thanks.
Chuck
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Something like this maybe?

Code:
Sub SplitNames()
Dim LR As Long, i As Long, X As Variant
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("A" & i)
        X = Split(.Value)
        .Offset(, 1).Resize(, UBound(X) + 1).Value = X
    End With
Next i
End Sub

Regards
John
 
Upvote 0
Thanks jolivanes, It prints to cells differently when I run it a few times. It seems to want a string in Cell A1, but after a few words it comes back with a run-time error '1004': Application-defined or object-defined error.

Where would I perhaps modify the code to have it look to pull the words out of a string, instead of a cell as I am assuming the code is looking for?

Thanks. - Chuck
 
Upvote 0
Code below I got to work for one row if the text is in cell A1. But wondering how I would modify below code if I want to use the string there instead?
Code:
Sub SplitNames()
 
Dim MyString
MyString = "These are the header words."
 
Dim LR As Long, i As Long, X As Variant
LR = Range("A" & Rows.Count).End(xlUp).Row
i = 1
    With Range("A" & i)
        X = Split(.Value)
        .Offset(, 1).Resize(, UBound(X) + 1).Value = X
    End With
 
End Sub
 
Upvote 0
This works! Now I'll play around with getting things to start in the right places etc. Thanks!

Code:
Sub SplitNames()
 
Dim MyString
MyString = "These are the header words."
 
Range("A1").Value = MyString
 
Dim LR As Long, i As Long, X As Variant
LR = Range("A" & Rows.Count).End(xlUp).Row
i = 1
    With Range("A" & i)
        X = Split(.Value)
        .Offset(, 1).Resize(, UBound(X) + 1).Value = X
    End With
 
End Sub
 
Upvote 0
Your code works great the way it was (once I understood what was going on). Change Offset #'s for destination row and col. Here is slight mods for using strings. Thanks again.
Code:
Sub SplitNames()
 
Range("A1").Value = "These are the header words."
Range("A2").Value = "And row below it."
 
Dim LR As Long, i As Long, X As Variant
LR = Range("A" & Rows.Count).End(xlUp).Row
 
For i = 1 To LR
    With Range("A" & i)
        X = Split(.Value)
        .Offset(, 0).Resize(, UBound(X) + 1).Value = X
    End With
Next i
 
End Sub
 
Upvote 0
This could be another way to consider.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> DoHeaders()<br>    <SPAN style="color:#00007F">Dim</SPAN> myHeaders<br>    <br>    myHeaders = Array("These are the header words", "And row below it")<br>    <SPAN style="color:#00007F">With</SPAN> Range("A1").Resize(UBound(myHeaders) + 1)<br>        .Value = Application.Transpose(myHeaders)<br>        .TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _<br>            ConsecutiveDelimiter:=True, Tab:=False, _<br>            Semicolon:=False, Comma:=False, Space:=True, Other:=False<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks Peter,

I like your use of an array, as the speed likely better than the writing to cells first. Although if screen activation is off, I would imagine the speed is nearly the same, as they both would have to write to the cells anyway eventually.

- Chuck
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,134
Members
452,890
Latest member
Nikhil Ramesh

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