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

by Allen Wyatt
(last updated May 3, 2014)

12

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

Using Delivery Point Barcodes

Printing barcodes on envelopes can help speed the delivery of your mail and possibly reduce what you pay for postage. ...

Discover More

Working with Record Numbers

Want to keep track of various rows in a data table through the use of record numbers? Here are some options and ...

Discover More

Locking a Field

When you use fields in your document, you may want them to not change from a particular displayed result. You can lock ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Selecting a Cell in the Current Row

Macros often need to select different cells in a worksheet. Here's how you can use macro commands to change which cell is ...

Discover More

Selecting a Specific Cell in a Macro

Need to use a macro to select a specific cell in a different workbook? It's not as straightforward of a proposition as ...

Discover More

Moving Macros from the Personal Workbook

Need to move a macro out of your Personal.xls workbook and into a regular workbook? You can do it using familiar editing ...

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}] 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 two minus 0?

2016-06-13 19:46:31

Nurmawan

Thanks for the short lesson. I get the points and it's working


2016-05-16 14:10:44

Matt

I would like to add the do not import column to this so that the data stays in the same cell. Any ideas?

Also, I want to set to different objectives but don't necessarily have a range for them besides (C:C). Any thoughts?


2016-02-11 22:01:29

Gii

Thank you very much, it worked
Have a great LIFE


2016-01-29 07:24:24

Barry

If you don't particularly need to dissect the string onto a spreadsheet then the SPLIT function will create a one-dimensional array in memory, and can be easier to manipulate in VBA code.

For more information on this: https://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx


2016-01-28 15:16:24

Jonathan Horovitz

Hi,

Sorry I figured this out. I was able to copy and paste the character "Space:=True, Other:=True, OtherChar:="?""

Thanks!


2016-01-28 14:59:26

Jonathan Horovitz

Hi,

How what do I write if i want to text-to-column using a special character "−". This is not a numerical negative sign but a special character being copied from pdf.

Thanks!

Jonahtan :)


2015-12-02 08:21:31

Mario

Rosen.
Thank you very much, it worked perfectly.
Have a great day.


2015-12-01 10:18:02

Rosen

Try inserting "Excel.Application.DisplayAlerts = False" (without the double quotes) before the line "Selection.TextToColumns _" then "Excel.Application.DisplayAlerts = True" (without the double quotes) after the line "OtherChar:="-""


2015-11-30 13:29:29

Mario

Hi
I need some help with this function, when I run de code, there is a message box where I need to accept the replacement.
How can I make a default to accept and the message box does not pop up?
Thanks in advance.


2015-03-19 17:18:37

Rosen

Graham, in answer to your question, yes. It is the same as if you used the Text To Column dialog feature on a cell or series of cells prior and then copied and pasted something new, it would parse based on whatever you set last.

If you don't want this to happen I would suggest that you run the same basic code again on a garbage cell (one not actually in use, but put some dumby information in first so it works) but change all the delimiters to False. (Or whatever you wish your default to be)


2015-03-16 17:50:58

Graham

I used a similar code to do this, but when I do so the DataType setting seems to get stuck. ie If I just paste text in, it insists on parsing as defined by the code until I shut Excel down, then I can paste in unparsed text again. Does this set some global variable within Excel?


2014-10-21 06:33:57

veeresh

I want to insert one columns in between the two columns and another one at another between two columns and also i want to delimit the data with delimiter "T"
Can anyone share Vb code for this


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.