Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later 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 March 11, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


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 (2929) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Comments Don't Appear when Cell is Pointed To

One way that you can view comments in a worksheet is to have them appear when you hover the mouse pointer over a cell. If ...

Discover More

Cannot Add Words to Dictionary

We all run across words that are spelled correctly, but that Word isn't aware of. The solution is to add those words to ...

Discover More

Removing Add-ins

Add-ins are used to extend Excel's capabilities in lots of different ways. If you want to get rid of an add-in ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Copying Pictures with a Macro

Copying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...

Discover More

Finding the Path to the Desktop

Figuring out where Windows places certain items (such as the user's desktop) can be a bit frustrating. Fortunately, there ...

Discover More

Creating and Naming a Worksheet Using a Macro

You can use macros to make your common Excel tasks easier and faster. For instance, if you routinely need to create new ...

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 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.