Excel VBA Split String by Delimiter: Step-by-Step Guide
In this article
To split a string by a delimiter in Excel VBA, use the built-in Split function: parts = Split(myString, "|"). This returns a zero-based array where each element is one piece of the original text. You can then read parts(0), parts(1), and so on, or loop through the array with LBound and UBound to write the pieces into cells. No add-ins or references are required; Split is part of the VBA runtime.
What You'll Need
- Microsoft Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows, or the Developer tab).
- A workbook where macros are enabled. Save it as
.xlsmif you want the code to persist. - Basic familiarity with opening the VBA Editor and inserting a module.
- Your source text and a clear idea of the delimiter (comma, pipe, semicolon, tab, space, etc.).
Menu labels in Excel and the VBA Editor can differ slightly by version, platform, and region, so if a label does not match exactly, look for the closest equivalent.
Step-by-Step
- Open the VBA Editor. Press Alt+F11 (Windows) or use Developer > Visual Basic. On Mac, use the Developer tab or Tools > Macro > Visual Basic Editor.
- Insert a standard module. In the VBA Editor, choose Insert > Module. A blank code window appears. Keep your code in a standard module, not in a sheet module, unless you specifically want sheet events.
- Write a simple test procedure. Type the following into the module:
This prints each piece to the Immediate Window (View > Immediate Window, or Ctrl+G).Sub SplitDemo() Dim s As String Dim parts() As String Dim i As Long s = "apple,banana,cherry" parts = Split(s, ",") For i = LBound(parts) To UBound(parts) Debug.Print i, parts(i) Next i End Sub - Run the procedure. Place the cursor inside the subroutine and press F5, or use Run > Run Sub/UserForm. Check the Immediate Window for three lines: 0 apple, 1 banana, 2 cherry.
- Write results to cells. Replace the
Debug.Printline with something likeCells(i + 1, 1).Value = parts(i). Because the array is zero-based, adding 1 puts the first piece in row 1. Adjust the column or starting row as needed. - Handle a different delimiter. Change the second argument of
Split. UseSplit(s, "|")for a pipe,Split(s, "\t")for a tab, orSplit(s, ";")for a semicolon. For a multi-character delimiter such as"--", pass the whole string; VBA treats it as a single separator. - Limit the number of pieces. Add a third argument to cap the result:
Split(s, ",", 3)returns at most three elements, with the remainder kept in the last element. This is useful when only the first few fields matter. - Loop through many rows. To split a whole column, read each cell in a loop, call
Split, and write the pieces to adjacent columns. For example:
This writes the pieces into columns B, C, D, and so on, starting from column A as the source.Sub SplitColumn() Dim lastRow As Long, r As Long, i As Long Dim parts() As String lastRow = Cells(Rows.Count, 1).End(xlUp).Row For r = 1 To lastRow parts = Split(CStr(Cells(r, 1).Value), ",") For i = LBound(parts) To UBound(parts) Cells(r, i + 2).Value = parts(i) Next i Next r End Sub - Save and test. Save the workbook as
.xlsm, close and reopen it, and run the macro again to confirm it behaves consistently.
Common Mistakes
- Assuming a one-based array.
Splitreturns a zero-based array. Referring toparts(1)when you meant the first item returns the second item, andparts(UBound(parts) + 1)raises a subscript out of range error. - Forgetting that the delimiter is case-sensitive in effect.
Splitmatches the exact characters you pass. Splitting on","will not split on";", and a space after the comma stays attached to the next piece unless you trim it. - Passing an empty delimiter.
Split(s, "")returns the whole string as a single element, not one character per element. If you need characters, loop throughMidinstead. - Using a non-string source. If the cell contains a number or date, convert it with
CStrbefore splitting, or you may get unexpected formatting. - Overwriting adjacent data. When writing pieces to columns, check that the target columns are empty; otherwise you silently replace existing values.
- Storing code in the wrong place. Code in a sheet module can behave differently from a standard module. Keep reusable procedures in a standard module.
Tips
- Trim each piece with
Trim(parts(i))to remove stray spaces around delimiters. - Use
Jointo reverse the operation:Join(parts, "|")rebuilds a string with a new delimiter. - Use
UBound(parts) - LBound(parts) + 1to count elements safely, rather than assuming a fixed number. - To split on several possible delimiters, replace them all with one character first, then split once. For example, replace semicolons and tabs with commas using
Replace, then callSplit. - For tab-delimited text copied from another program, split on
vbTabrather than a literal tab character, which is easy to lose in code. - Test with edge cases: an empty string, a string with no delimiter, and a string ending in the delimiter.
Splitreturns a single-element array for an empty string and includes an empty final element when the string ends with the delimiter.
Troubleshooting
If the macro does nothing, confirm macros are enabled and that you are running the correct procedure. If you see a "Subscript out of range" error, check your array indexes; the most common cause is treating the array as one-based. If the output looks like the whole string in one cell, the delimiter probably does not match the text exactly, so inspect the source for hidden spaces or a different character. If results appear in the wrong cells, verify the row and column offsets in your write statement. If the Immediate Window stays empty, open it from View > Immediate Window. If the workbook will not keep the macro after closing, save it as .xlsm. If a step deletes or overwrites data, undo is not available for macro actions, so test on a copy of the workbook first.
Frequently Asked Questions
Is the Split function zero-based or one-based?
Zero-based. The first element is parts(0). Use LBound and UBound to loop safely.
Can I split on more than one delimiter at once?
Not directly. Replace the extra delimiters with a single character using Replace, then call Split once.
What happens if the delimiter is not found?
Split returns an array with one element containing the entire original string.
Can I limit how many pieces are returned?
Yes. Pass a third argument, such as Split(s, ",", 3), to cap the result at three elements, with the remainder in the last one.
Does Split work with tabs and line breaks?
Yes. Use vbTab for tabs and vbCrLf or vbLf for line breaks, depending on how the text was created.
Why does my split leave spaces at the start of each piece?
The delimiter is a comma, but the source has a comma followed by a space. Wrap each piece in Trim to remove the leading space.