Table of Contents
Converting text to columns in VBA refers to the process of separating a single column of text into multiple columns, with each column containing a specific segment of the original text. This can be achieved by using the “Text to Columns” function in VBA, which allows for the customization of the delimiter used to separate the text, as well as the number and format of the resulting columns. This feature is particularly useful for organizing and manipulating large amounts of data in a spreadsheet. By utilizing VBA, users can automate the process of converting text to columns, saving time and effort in data analysis and organization.
VBA: Convert Text to Columns
You can use the Range.TextToColumns method in VBA to convert a cell of text into columns.
The following examples show how to use this method in practice in different scenarios.
Example 1: Convert Text to Columns (Space as Delimiter)
Suppose we have the following list of names in the range A1:A9 in Excel:

We can create the following macro to convert the text in each cell into separate columns, using spaces as delimiters:
Sub TextToCols()
Range("A1:A9").TextToColumns _
ConsecutiveDelimiter:=True, _
Space:=True
End SubWhen we run this macro, we receive the following output:

Notice that the text in each cell has been converted into separate columns.
Note: The argument ConsecutiveDelimiter:=True tells VBA to consider consecutive delimiters together as one single delimiter.
Example 2: Convert Text to Columns (Comma as Delimiter)
Suppose we have the following list of names in the range A1:A9 that are separated by commas:

We can create the following macro to convert the text in each cell into separate columns, using commas as delimiters:
Sub TextToCols()
Range("A1:A9").TextToColumns _
ConsecutiveDelimiter:=True, _
Comma:=True
End SubWhen we run this macro, we receive the following output:

Notice that the text in each cell has been converted into separate columns.
Example 3: Convert Text to Columns (With Text in Double Quotes)
Suppose we have the following list of names in the range A1:A9 that are separated by spaces and have double quotes:

We can create the following macro to convert the text in each cell into separate columns:
Sub TextToCols()
Range("A1:A9").TextToColumns _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Space:=True
End SubWhen we run this macro, we receive the following output:

Notice that the text in each cell has been converted into separate columns.
Note that we used the argument TextQualifier:=xlDoubleQuote to tell VBA that the text was surrounded by double quotes.
Note: You can find the complete documentation for the TextToColumns method in VBA .
The following tutorials explain how to perform other common tasks using VBA:
Cite this article
stats writer (2024). How can I convert text to columns in VBA?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-text-to-columns-in-vba/
stats writer. "How can I convert text to columns in VBA?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-text-to-columns-in-vba/.
stats writer. "How can I convert text to columns in VBA?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-text-to-columns-in-vba/.
stats writer (2024) 'How can I convert text to columns in VBA?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-text-to-columns-in-vba/.
[1] stats writer, "How can I convert text to columns in VBA?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert text to columns in VBA?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
