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

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

Quickly Transposing Cells

If you want to turn a range of cells by 90 degrees within a worksheet, you need to understand how Excel can handle the ...

Discover More

Controlling Where You Edit Cell Contents

You can edit cell information either in the Formula bar or in the cell itself. Here's how you can configure Excel to specify ...

Discover More

Updating the Spelling Exclusion List Automatically

Want to add words easily to the spelling exclusion list? Here's a macro that can make the task completely painless.

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 (menu)

Renaming a Macro

Got a macro that doesn't have quite the right name? You can rename the macro by following these simple steps.

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in cells; ...

Discover More

Deriving the Worksheet Name

Excel doesn't provide an easy way to grab the worksheet name for use within a worksheet. Here are some ideas on ways you can ...

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 8Mpixels. 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 - 4?

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.