Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Delimited Text-to-Columns in a Macro.

Delimited Text-to-Columns in a Macro

Written by Allen Wyatt (last updated April 20, 2019)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

One of the handiest features in Excel is the Text to Columns feature, which allows you to easily split cell contents into individual cells according to any criteria you specify. One method of using the feature is to allow it to recognize characters within the cells and use those characters to trigger where the split should take place. This type of splitting is referred to as a delimited split.

You may be wondering how you can perform a delimited text-to-columns operation in a macro you may be writing. This is easy enough to do by using the TextToColumns method on a selection you set up. Consider the following very simple macro:

Sub ExampleSplit1()
    Selection.TextToColumns _
      Destination:=Range("A2"), _
      DataType:=xlDelimited, _
      TextQualifier:=xlDoubleQuote, _
      ConsecutiveDelimiter:=False, _
      Tab:=True, _
      Semicolon:=False, _
      Comma:=False, _
      Space:=False, _
      Other:=True, _
      OtherChar:="-"
End Sub

Notice all the variables that you can set for the TextToColumns method. Most of these variables are only necessary because this is a delimited split; the variables set what is used as a delimiter by the method. Beginning with the Tab line, the variables correspond directly to the settings you would make in Step 2 of the Convert Text to Columns Wizard, if you were manually using the feature. You can set Tab, Semicolon, Comma, and Space to either True or False, depending on whether you want that character used as a delimiter.

You can also set the Other variable to True or False, depending on whether you want to have a "user defined" delimiter. If you set it to True, then you should set the OtherChar variable equal to the character you want used as a delimiter.

If you use the TextToColumns method multiple times in the same macro, the only thing you need to do on invocations subsequent to the first is to change variables that differ from the previous invocation. For instance, let's say that you are calling the method twice in the same macro, and the first time you want the split to be on an instance of the dash character, but the second you want it to be on any instance of a lowercase x. You can put the macro together like this:

Sub ExampleSplit2()
    Dim objRange1 As Range
    Dim objRange2 As Range

    'Set up the ranges
    Set objRange1 = Range("A2:A20")
    Set objRange2 = Range("A21:A35")

    'Do the first parse
    objRange1.TextToColumns _
      Destination:=Range("A2"), _
      DataType:=xlDelimited, _
      Tab:=False, _
      Semicolon:=False, _
      Comma:=False, _
      Space:=False, _
      Other:=True, _
      OtherChar:="-"

    'Do the second parse
    objRange2.TextToColumns _
      Destination:=Range("A21"), _
      DataType:=xlDelimited, _
      OtherChar:="x"
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8317) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Delimited Text-to-Columns in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Replacing Text Selections

When editing a document, Word normally replaces whatever text you select with whatever you start to type. Here's how to ...

Discover More

Calculating an Age On a Given Date

Start putting dates in a worksheet (especially birthdates), and sooner or later you will need to calculate an age based ...

Discover More

Creating an Index Entry

In order to create an index, you first need to create the entries that will be used to compile the index. Here's how you ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Limiting Scroll Area

If you need to limit the cells that are accessible by the user of a worksheet, VBA can come to the rescue. This doesn't ...

Discover More

Reversing Cell Contents

Macros are great at working with text. This tip presents an example that shows this versatility by reversing the contents ...

Discover More

Friendly and Informative Error Handling

When creating macros, it is helpful to know what is going on within the macro itself in case an error crops up. Here's ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2019-08-02 09:01:07

Dennis Costello

The fact that the parameters are "sticky" - that TextToColumns - e.g. if the comma parameter is elided, it will have the same value as the last time it was invoked. That stickiness extends to the spreadsheet action, too - if you do TextToColumns with space delimiting in VBA, space delimiting will be in place the next time you do a TextToColumns in the DataTools box on the Data ribbon. More insidiously, it will also apply the next time you just paste something into a column. So it's a good practice in VBA to do a "null" TextToColumns at the end of your macro, turning off all these parameters.

Been burned by this in the past...

On a related note, one might wish, when using TextToColumns in a macro, to find out what the current sticky settings are before changing them in your invocation, so you can do the "null" TextToColumns in a way that restores those parameters. I'm sure there's a set of properties somewhere containing this (perhaps read-only), but I've never been able to find them.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.