Excel VBA Split Array: Step-by-Step Guide
In this article
Use the Split function in Excel VBA to turn a delimited string into a zero-based array of substrings in one line — for example parts = Split("red,green,blue", ",") gives you parts(0) through parts(2). There is no built-in "split an existing array" method in VBA, so if you need to break an array into chunks you loop through it and copy elements into new arrays. This guide covers both: the real Split function for strings, and the loop pattern for slicing arrays.
What You'll Need
- Excel on Windows or Mac with the Visual Basic Editor available (Alt+F11 on Windows, Option+F11 on Mac; labels and shortcut behaviour can differ slightly by version and region).
- A workbook saved as .xlsm if you want your macro to persist — .xlsx strips VBA on save.
- Basic familiarity with the VBA Editor: inserting a module and pressing F5 to run.
- Macro security set to allow macros in this workbook (File > Options > Trust Center > Trust Center Settings > Macro Settings on Windows; Excel > Preferences > Security on Mac).
Step-by-Step
- Open the editor. Press Alt+F11 (Windows) or Option+F11 (Mac) to open the Visual Basic Editor.
- Insert a module. In the Project Explorer, right-click your workbook and choose Insert > Module. Code typed in a sheet module works too, but a standard module keeps things tidy.
- Declare your variables. Start with
Dim parts() As Stringfor the output array,Dim s As Stringfor the input, andDim i As Longfor the loop counter. UseLongrather thanInteger; VBA arrays can exceed the Integer limit easily. - Call Split on a string. Write
s = "apple,banana,cherry"thenparts = Split(s, ","). The second argument is the delimiter. Omit it and VBA splits on a single space. The result is a zero-based array:parts(0)is "apple",parts(2)is "cherry". - Find the upper bound. Use
UBound(parts)to get the last index andLBound(parts)for the first. For a Split result, LBound is always 0. Loop withFor i = LBound(parts) To UBound(parts)so the code survives an empty result. - Handle the limit argument.
Split(s, ",", 3)returns at most three elements; the third contains the rest of the string unsplit. This is useful when your data has a fixed prefix followed by free text. - Split on multiple characters. The delimiter is a string, not a single character:
Split(s, " | ")works. If you need several different delimiters, run Split repeatedly or useReplaceto normalise them to one delimiter first. - Slice an existing array into chunks. VBA has no native array-slice function. To split an array of 100 items into groups of 10, declare
Dim chunk() As Variant, size it withReDim chunk(0 To 9), copy elements in a nested loop, then process each chunk before reusing it. Alternatively build a jagged structure:Dim chunks() As Variant,ReDim chunks(0 To 9), and assign each inner array withchunks(k) = someVariantArray. - Write results out. To dump the array to a worksheet, use
Range("A1").Resize(UBound(parts) + 1, 1).Value = Application.Transpose(parts). Transpose handles a one-dimensional array; for very large arrays it can fail, so loop and assign cell by cell instead. - Run and check. Press F5, or step through with F8 to watch values in the Locals window. Confirm the count matches your expectation before wiring the macro into a button.
Common Mistakes
- Assuming one-based indexes. Split returns a zero-based array. Reading
parts(1)when you meant the first item is the classic off-by-one bug. - Using UBound without checking for an empty string.
Split("", ",")returns an array with one empty element, not an empty array. Test the input first if blank values are possible. - Forgetting that Split only works on strings. Passing a number or a range object raises a type mismatch. Convert with
CStrfirst. - Declaring the output as a fixed-size array. Split returns a dynamic array; declare it with empty parentheses and let Split size it.
- Expecting Split to handle a Variant array of mixed types. It will coerce, sometimes unexpectedly. Convert each element to a string explicitly when types are mixed.
- Using Transpose on a two-dimensional array. It errors. Loop instead, or write row by row.
Tips
- Trim each element with
Trim(parts(i))when your source data has spaces after commas. - Use
Join(parts, ",")to reverse the operation and rebuild a string. - For CSV files, use
OpenandLine Inputthen Split each line — but be aware Split does not respect quoted commas. For quoted CSV, use Power Query or the Text Import Wizard instead. - Use
Filter(parts, "keyword")to pull matching elements without writing a loop. - Keep a helper function that returns the chunk count so your slicing loop stays readable.
- Add
Option Explicitat the top of every module to catch undeclared variables early.
Troubleshooting
"Subscript out of range" on parts(i). You indexed past UBound. Print UBound in the Immediate window with Debug.Print UBound(parts) and compare.
"Type mismatch". The variable you passed to Split is not a string. Wrap it in CStr, or check whether the cell is empty.
Split returns one element containing the whole string. Your delimiter does not match the data. Watch for non-breaking spaces, tabs, or a semicolon where you expected a comma.
Transpose fails. The array is too large or two-dimensional. Loop and assign cells individually.
Macro will not run. Check macro security settings and confirm the file is .xlsm. If the workbook came from another source, you may need to unblock it in File > Info before macros are allowed.
If that did not work, rebuild the test in a fresh module with a hard-coded string. If the hard-coded version works, the problem is in your data source, not the Split call.
Frequently Asked Questions
Does VBA have a built-in function to split an array into smaller arrays?
No. Split works on strings only. To chunk an array you write a loop that copies elements into new arrays or into a jagged array of arrays.
Is the array returned by Split zero-based or one-based?
Always zero-based. LBound returns 0 and UBound returns the count minus one, unless you pass an empty string, which yields a single empty element.
Can I split on more than one delimiter at once?
Not directly. Replace secondary delimiters with your main one first, or call Split repeatedly on each result.
How do I split a string into individual characters?
Pass an empty string as the delimiter: Split("abc", "") returns three elements. Alternatively loop with Mid.
Why does Split sometimes return an extra empty element?
Because the string ends with the delimiter, or contains two delimiters in a row. Filter out empty strings with a loop or Filter before processing.
Can Split handle quoted CSV values containing commas?
No. Split treats every comma as a delimiter, including ones inside quotes. Use Power Query or the Text Import Wizard for proper CSV parsing.