Excel VBA: Parse a String by Delimiter (Step by Step)
In this article
To parse a string by a delimiter in Excel VBA, use the built-in Split function: parts = Split(myString, ",") returns a zero-based array where each element is one field. Loop from LBound(parts) to UBound(parts) and write each element to a cell, a variable, or a collection. That single line handles the vast majority of comma-, pipe-, semicolon- and tab-separated text, and the rest of this guide covers the edge cases that trip people up.
What You'll Need
- Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows, or Developer > Visual Basic on the ribbon). If the Developer tab is hidden, enable it via File > Options > Customize Ribbon and tick Developer in the right-hand list; labels vary slightly by version and region.
- A workbook saved as .xlsm (macro-enabled) if you want to keep the code. Saving as .xlsx strips macros.
- Sample text with a delimiter in a cell, for example
Smith|John|42|Londonin cell A1. - Basic familiarity with opening a module: in the VBA Editor, Insert > Module.
Step-by-Step
- Open the editor and add a module. Press Alt+F11, then choose Insert > Module. A blank code pane appears. This is where the macro lives; code placed in a sheet module also works but is harder to reuse.
- Write a minimal Sub. Start with
Sub ParseText()and end withEnd Sub. Declare your variables withDim: aStringfor the source text, aVariantfor the array, and aLongfor the loop counter. Declaring the array asVariantmatters becauseSplitalways returns a Variant array of strings. - Read the string into a variable. For example
myString = Range("A1").Value. Reading from a cell is safer than hard-coding text because you can change the source without editing code. If the cell is empty,Splitreturns a one-element array containing an empty string, so guard against that if it matters. - Call Split with your delimiter.
parts = Split(myString, "|"). The second argument is the delimiter and it is case-sensitive for letters but exact for symbols. Omit the second argument entirely and VBA assumes a space character, which is rarely what you want. - Loop through the results. Use
For i = LBound(parts) To UBound(parts). Always useLBoundandUBoundrather than assuming 0 and a fixed count; the lower bound can be changed withOption Base, and the upper bound depends entirely on how many delimiters were found. - Write each element out. Inside the loop, place each piece where you need it:
Cells(1, i + 2).Value = parts(i)spills the fields across row 1 starting at column B. Alternatively assign to named variables when the field count is fixed, for examplelastName = parts(0)andfirstName = parts(1). - Trim whitespace. Wrap each element in
Trim()before using it:Cells(1, i + 2).Value = Trim(parts(i)). Text exported from other systems often has a space after each comma, and without trimming those spaces end up in your data and break lookups. - Handle a multi-character delimiter.
Splitaccepts a string of any length as the delimiter, soSplit(myString, " - ")works. What it does not do is treat several different characters as alternatives; for that you need a different approach (see the tips below). - Run and test. Press F5 with the cursor inside the Sub, or use Run > Run Sub. Check the output cells against the source string. Test with a trailing delimiter, a doubled delimiter, and no delimiter at all before trusting the code on real data.
- Save the workbook as .xlsm. Use File > Save As and pick Excel Macro-Enabled Workbook. If you skip this, Excel warns you on close that macros will be lost.
Common Mistakes
Assuming the array starts at 1. Split returns a zero-based array by default, so parts(1) is the second field, not the first. Off-by-one errors here are the single most frequent cause of wrong output.
Hard-coding the upper bound. Writing For i = 0 To 3 breaks the moment a row has four fields instead of five, and silently drops data when it has six. Always derive the count from UBound.
Using the wrong delimiter character. A comma and a semicolon look similar in a hurry, and tab characters are invisible. If output lands entirely in one element, the delimiter is almost certainly wrong. Check with Debug.Print Len(myString) and inspect the actual characters.
Forgetting that Split returns text. Every element is a string, even "42". Writing it to a cell usually converts automatically, but arithmetic in VBA on the raw element can behave unexpectedly. Convert explicitly with CLng, CDbl or CDate when the field is numeric or a date.
Ignoring empty elements. Split("a,,b", ",") returns three elements, the middle one empty. Code that assumes every element has content will produce blank cells or fail on conversion.
Leaving the delimiter argument off. Split(myString) splits on spaces only, which mangles names and addresses.
Tips
Use a named constant for the delimiter at the top of the module, such as Const DELIM As String = "|", so changing the format later means editing one line.
To handle several possible delimiters, replace them all with one first: myString = Replace(myString, ";", ",") and repeat for each variant, then split on the comma. This is simpler and more readable than writing a character-by-character parser.
If a field can itself contain the delimiter (a quoted address with a comma, for instance), plain Split is not enough because it has no concept of quoting. In that case import the text with Data > From Text/CSV, which understands quoted fields, rather than parsing it in VBA.
For large volumes, read the whole range into a Variant array in one go, process in memory, and write the results back in one assignment. Looping cell by cell is dramatically slower on thousands of rows.
Use Debug.Print and the Immediate window (Ctrl+G) to inspect each element while developing; it is faster than writing to cells and deleting afterwards.
Troubleshooting
Nothing splits. The delimiter does not match. Confirm the exact character, including whether it is a comma, a semicolon or a tab. On non-English regional settings, exported CSV files often use semicolons.
Runtime error 9, subscript out of range. You are referencing an index the array does not have, usually because the string had fewer delimiters than expected. Check UBound(parts) before indexing, or test If UBound(parts) >= 2 Then.
Extra blank fields appear. The source has doubled or trailing delimiters. Either clean the string first with Replace, or skip empty elements inside the loop with If Len(Trim(parts(i))) > 0 Then.
Numbers arrive as text. Convert explicitly, or set the destination cells' number format before writing. A leading apostrophe or a cell pre-formatted as Text will keep values as text.
Macro does not run at all. Check that macros are enabled for the file and that the workbook is in a trusted location. If the code is in a sheet module rather than a standard module, the Sub may not appear in the macro list; move it to a standard module via Insert > Module.
Output overwrites itself. If you write to the same cells each run without clearing them, stale values remain where the new string has fewer fields. Clear the target range at the start of the macro.
Frequently Asked Questions
Does Split work with more than one delimiter character?
Yes, as long as they form one contiguous sequence. Split(text, "::") treats the double colon as a single delimiter. It cannot treat two unrelated characters as alternatives.
Is the returned array zero-based?
Yes, by default the first element is index 0. Use LBound and UBound so your code stays correct regardless.
Can I split into a fixed number of parts?
Not directly. Split returns every piece. If you need to keep the remainder intact, use InStr to find the first delimiter, take Left and Mid around it, and repeat only as many times as needed.
What happens with an empty string?
Split("", ",") returns a single-element array containing an empty string. Test for emptiness before processing if blank input is possible.
Can I split on a line break?
Yes. Use vbCrLf, vbLf or vbCr as the delimiter, depending on how the source text was created. Windows text usually uses vbCrLf.
Does this work in Excel for Mac?
The Split function and the VBA Editor are available on Mac Excel, though the editor layout and some shortcut keys differ. The code itself is the same.