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:
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.
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!
Workbooks can contain macros, or not. It is entirely up to you whether they do or not, but at some future time you might ...
Discover MoreNeed to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...
Discover MoreExcel doesn't provide an easy way to grab the worksheet name for use within a worksheet. Here are some ideas on ways you ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-12-06 18:23:02
Hello, when I try to program the delimiter "other" in a macro and use the "↔" symbol, when I restart the program, the macro changed the symbol to "?" and it doesn't work. Do you have any ideas on how to fix?
2018-11-24 05:00:24
Naveen
Hi Allen,
I just looking for a macro can do the position delimited text file in to excel file.
Could you please help me out.
Thanks and regards,
Naveen.
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments