Excel.Tips.Net ExcelTips (Menu Interface)

Delimited Text-to-Columns in a Macro

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.

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, _
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, _

    'Do the second parse
    objRange2.TextToColumns _
      Destination:=Range("A21"), _
      DataType:=xlDelimited, _
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.

Related Tips:

More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want. Check out ExcelTips: The Macros today!


Comments for this tip:

Nilesh K Patel    13 Nov 2013, 05:33

Please suggest me how to use column to character. i.e. nilesh k patel, now want to use the same name in other sheet 'N'character in one column, 'i' in other column, 'l' in other ..... so on. Please suggest me the formula and macro use for the same
Azbi    06 Jul 2013, 01:02
I have a problem like this

This the file that I have :

UNA:+.? 'UNB+UNOA:2+TPS+MSC+130706:1101+07061'UNH+07061+CODECO:D:95B:UN:ITG14+SISI021'BGM+34+SISI021201307061192+9'TDT+20+HB327R+1+13+MSC:172:20+++9HA276:103::SIMA SINGAPORE'RFF+VON:HB327R'LOC+9+IDSUB:139:6+TPS:GAT:ZZZ'DTM+178:190012312300:203'DTM+133:190012312300:203'NAD+CF+MSC:160:ZZZ'

I only need 4 column from this data, that contain a data like this MSCU9283253|4500|411IN1306177|201307061031
MSCU9283253 id a kind of container number
4500 is a kind of container type
411IN1306177 is a kind of booking number
but for column that contain "201307061031" should be split "20130706" as a date with a format "YYYYMMDD" and "1031" is an hour with a format "hh:ss"
So the total become 5 columns

Any Idea?
Sunil    14 Mar 2013, 02:51
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;psh2s2.d", Destination:=Range("$D$1"))
        .Name = "psh2s2.d"
        .FieldNames = True
        .PreserveFormatting = True
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileTabDelimiter = True
        .TextFileCommaDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

In the last line
".Refresh BackgroundQuery:=False"
I'm getting error as
"Run-time error '1004'
Excel cannot find the text file to refresh this external data range"

Can you please help me on this??
raajaa    26 May 2012, 23:38
thanks sir
Bogsy    22 May 2012, 21:55
NICE... makes life easier...
Thanks alot.

Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 3+4? (To prevent automated submissions and spam.)
          Commenting Terms

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2013)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2013)

Our Products

Premium Newsletters

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2014 Sharon Parq Associates, Inc.