How to use the TEXTTOCOLUMNS method
The TextToColumns method puts a cell that contains a string into substrings and distributes them horizontally using a delimiting character or based on a fixed width.
Table of Contents
1. TEXTTOCOLUMNS Method Syntax
expression.TextToColumns (Destination, DataType, TextQualifier, ConsecutiveDelimiter, Tab, Semicolon, Comma, Space, Other, OtherChar, FieldInfo, DecimalSeparator, ThousandsSeparator, TrailingMinusNumbers)
2. TEXTTOCOLUMNS Method Arguments
parameter | Description |
Destination | Optional. A Range object that points to a location on a worksheet. |
DataType | Optional. The text format of the source data. |
TextQualifier | Optional. Text qualifiers: single, double, or no quotes. |
ConsecutiveDelimiter | Optional. True - Consecutive delimiters as one delimiter. The default value is False. |
Tab | Optional. True - DataType be xlDelimited and the tab character is a delimiter. The default value is False. |
Semicolon | Optional. True - DataType be xlDelimited, the semicolon is a delimiter. The default value is False. |
Comma | Optional. True -Â DataType be xlDelimited, the comma is a delimiter. The default value is False. |
Space | Optional. True -Â DataType be xlDelimited, the space character is a delimiter. The default value is False. |
Other | Optional. True -Â DataType be xlDelimited, the character specified by the OtherChar argument is the delimiter. The default value is False. |
OtherChar | Optional. Required if Other is True; the delimiter character when Other is True. |
FieldInfo | Optional. An array containing parse information for the individual columns of data. The interpretation depends on the value of DataType. When the data is delimited, this argument is an array of two-element arrays, with each two-element array specifying the conversion options for a particular column. The first element is the column number (1-based), and the second element is one of the XlColumnDataType constants specifying how the column is parsed. |
DecimalSeparator | Optional. A decimal separator that Excel uses to recognize numbers. The default setting is the system setting. |
ThousandsSeparator | Optional. The thousands separator that Excel uses to recognize numbers. The default setting is the system setting. |
TrailingMinusNumbers | Optional. Negative numbers, in other words, numbers that begin with a minus character. |
3. Record macro while running TextToColumns Wizard?
The "Text to columns" feature in excel lets you split multiple values in a single cell using a delimiting character, across columns.
Where is this button?
- Go to tab "Data" on the ribbon.
- Press with left mouse button on the "Text to columns" button.
A dialog box appears, see the animated image above.
What happens if we record a macro while performing this action? This is what the macro recorder returns:
Sub Macro1() Selection.TextToColumns Destination:=Range("A1"), _ DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, _ Tab:=False, _ Semicolon:=False, _ Comma:=True, _ Space:=False, _ Other:=False, _ FieldInfo _ :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), _ TrailingMinusNumbers:=True End Sub
4. TextToColumns method - dot as delimiting character
The image above shows a cell containing colors separated by a dot. The following VBA macro splits the string into substrings and distributes them to a cell each horizontally.
Sub Macro4() Selection.TextToColumns Destination:=Range("B2"), _ DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, _ Tab:=False, _ Semicolon:=False, _ Comma:=False, _ Space:=False, _ Other:=True, _ OtherChar :=".", _ FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), _ Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True End Sub
5. TextToColumns method - comma as delimiting character
Sub Macro3() Selection.TextToColumns Destination:=Range("B2"), _ DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=True, _ Tab:=False, _ Semicolon:=False, _ Comma:=False, _ Space:=True, _ Other:=False, _ FieldInfo :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), _ Array(5, 1)), TrailingMinusNumbers:=True End Sub
6. Where to put the code?
- Press Alt + F11 to open the VIsual Basic Editor (VBE).
- Press with left mouse button on "Insert" on the top menu.
- Press with left mouse button on "Module" to create a new module.
- Copy and paste VBA code to the module.
- Return to Excel.
Excel categories
6 Responses to “How to use the TEXTTOCOLUMNS method”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Hi Oscar,
To avoid array formula,and use Text to columns, can be used:
Sub Splitcell()
[A1].TextToColumns [A1].Offset(, 2), xlDelimited, , , 0, 0, 1, 0, 0
End sub
PS
Your site is excellent. One of the best of the best.
Ingolf
Ingolf,
thank you!
Two points about your SplitValues UDF...
First, when the Split function will be called only once during a procedure, there is no need to assign the output from the Split function to an intermediary dynamic array in order to work with its output (doing so similar to selecting a range and then woring with the Selection object instead of the range itself); rather, you can work directly with the Spiit function itself...
Second, the way you have implemented your function, you need to know, in advance, how many substrings the 'b' argument will split into so that you can select exactly that amount of cells to array-enter your formula in, otherwise, any excess selected cells will display the #N/A error which, of course, looks kind of ugly but would be necessary if the text passed into the 'b' argument could vary in the number of delimiters over time. We can eliminate the #N/A errors for any excess selected cells fairly easily using the Application.Caller object like so...
Rick Rothstein (MVP - Excel),
Interesting comment, as always.
Your second point was new to me.
[quote]
Second, the way you have implemented your function, you need to know, in advance, how many substrings the 'b' argument will split into so that you can select exactly that amount of cells to array-enter your formula in, otherwise, any excess selected cells will display the #N/A error which, of course, looks kind of ugly but would be necessary if the text passed into the 'b' argument could vary in the number of delimiters over time.
[/quote]
Not too sure..
Sub SplitCell()
With Range("A1", Cells(Rows.Count, "A").End(xlUp))
.TextToColumns .Offset(, 2), xlDelimited, , , 0, 0, 1, 0, 0
End With
End Sub
I was referring to problems associated with implementing the UDF in my previous posting... macros (like what you posted) are completely different than UDFs and do not have the same (or virtually any such) restrictions. As for determining the vertical extent of the cells to apply the Text-To-Columns method to... that is not necessary as Text-To-Columns will only work on cells with data, so applying it to the entire column will work the same as restricting it to your calculated range...
Columns("A").TextToColumns .Offset(, 2), xlDelimited, , , 0, 0, 1, 0, 0