Simple VBA

normpam

Active Member
Joined
Oct 30, 2002
Messages
355
Have a first and last name in cell C10. Recorded a macro to use TextToColumns.

Selection.TextToColumns Destination:=Range("C10"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _

I would like to be able to run the macro for any cell, so I changed the "C10" in the first line above to ActiveCell, ActiveCell thinking it would work... but I get an error. What am I missing here?

Thanks!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
I tested this code out with "A B C" in cell A1. It worked an after the code ran "A" is in A1, "B" is in B1, and "C" is in C1. What error are you getting?

Code:
Sub Test()
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False
End Sub
 
Upvote 0
Much thanks! It's almost always the syntax that gets me! I thought by replacing the "C10" address with the ActiveCell... it would work. But as you show, the syntax needs to be without the parentheses.
 
Upvote 0
I tested this code out with "A B C" in cell A1. It worked an after the code ran "A" is in A1, "B" is in B1, and "C" is in C1. What error are you getting?

Code:
Sub Test()
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False
End Sub
If the number of "names" in the selected cell is not fixed (as your example suggests), one can use this simpler code line as long as some maximum number of names can be set (here I assume no more than 99 names will ever appear in the selected cell)...
Code:
Sub Test1()
  Selection.Resize(, 99) = Split(Selection & Space(99))
End Sub
However, the OP seemed to suggest that there would always only be two names in the cell. If that is always true, then my code can be reduced to this)...
Code:
Sub Test()
  Selection.Resize(, 2) = Split(Selection & "  ")
End Sub
Note that there are two space characters between those quote marks.
 
Upvote 0

Forum statistics

Threads
1,215,140
Messages
6,123,267
Members
449,093
Latest member
Vincent Khandagale

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