Split text across columns
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
- Click "Text to columns" button
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
The TextToColumns method and its parameters is explained here: Range.TextToColumns Method (Excel)
You an use the TextToColumns method in a macro but what about a user defined function? Unfortunately, you can´t use this method in a function. Don´t give up, it is easy to build a basic udf that splits values across columns.
Function SplitValues(a As String, b As String) Dim Text() As String Text = Split(b, a) SplitValues = Text End Function
How to use it
- Select cell range C1:E1
- Type =SplitValues(",",A1) in the formula bar
- Press and hold CTRL + SHIFT
- Press Enter
If you did this right, the formula has now a leading { and an ending }, like this {=SplitValues(",",A1)}. They appear automatically, don´t type them.
Build a user defined function
To build a user defined function, follow these steps:
- Press Alt + F11 to open the visual basic editor
- Click "Insert" on the menu
- Click "Module"
- Copy the code above and paste it to the code module.
Explaining the user defined function
Function name and arguments
A user defined function procedure always start with "Function" and then a name. This udf has two arguments, a and b. Both a and b are strings.
Function SplitValues(a As String, b As String)
Declaring variables
Dim Text() As String
Text() is a dynamic string array. Read more about Defining data types.
Split function
Text = Split(b, a)
The Split function accepts a text string and returns a zero-based, one-dimensional array containing all sub strings. Split allows you also to specify a delimiting character, default is the space character. The substrings are stored as multiple strings in Text array.
The udf returns the substrings in Text
SplitValues = Text
End a udf
End function
A function procedure ends with the "End function" statement.
Recommended reading
- Count the number of cells within a range that match multiple comma separated values
- Excel udf: Lookup and return multiple values concatenated into one cell
- Excel udf: Filter common values between two cell ranges in excel
Download excel *.xlsm file
How to count word frequency in a cell range [UDF]
This user defined function creates a unique distinct list of words and how many times they occur in the selected […]
Extract unique distinct values from a filtered Excel defined Table [UDF and Formula]
Robert Jr asks: Oscar, I am using the VBA code & FilterUniqueSort array to generate unique lists that drive Selection […]
List files in a folder and subfolders [UDF]
This article demonstrates a user defined function that lists files in a ggiven folder and subfolders. A user defined function is […]
Search for a file in folder and subfolders [UDF]
The image above demonstrates a user-defined function in cell range B6:D7 that allows you to search a folder and subfolders […]
Split words in a cell range into a cell each [UDF]
This post describes how to split words in a cell range into a cell each using a custom function. I […]
Search all workbooks in a folder
Today I'll show you how to search all Excel workbooks with file extensions xls, xlsx and xlsm in a given folder for a […]
This article demonstrates a macro that copies values between sheets. I am using the invoice template workbook. This macro copies […]
Apply dependent combo box selections to a filter
Josh asks: now if i only knew how to apply these dependent dropdown selections to a filter, i'd be set. […]
Open Excel files in a folder [VBA]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
Create links to all sheets in a workbook
The macro demonstrated above creates hyperlinks to all worksheets in the current worksheet. You will then be able to quickly […]
6 Responses to “Split text across columns”
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