Excel VBA String Variable Maximum Length Explained
In this article
A VBA String variable can hold up to about 2 billion characters (2^31 - 1), but the practical limit is far lower: a cell can hold only 32,767 characters, and a text box or message box will choke long before that. In other words, the data type itself is not usually your bottleneck — Excel's UI and object model are. The shortest working rule: declare Dim s As String, keep strings under 32,767 characters if they must land in a cell, and split or stream anything larger.
This guide explains the real limits, how to test them, and how to handle long text without hitting errors. Menu labels and dialog wording can differ slightly by Excel version and region; the steps below use the standard desktop ribbon.
What You'll Need
- Excel for Windows or Mac desktop (VBA is not available in Excel for the web).
- The Developer tab enabled: File > Options > Customize Ribbon > check Developer. On Mac: Excel > Preferences > Ribbon & Toolbar.
- A macro-enabled workbook (.xlsm) so your code is saved.
- Basic familiarity with the VBA editor (Alt+F11 on Windows, Option+F11 on Mac).
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project pane, right-click your workbook and choose Insert > Module.
- Declare a String variable. Type
Dim myText As String. A fixed-length declaration such asDim code As String * 10caps the value at 10 characters and silently pads or truncates — avoid it unless you specifically want that behavior. - Understand the theoretical ceiling. A variable-length String in VBA can address roughly 2^31 characters, but you will never reach that in Excel because every place you display or store the text has a smaller cap. Treat ~2 billion as trivia, not a target.
- Know the cell limit. A single worksheet cell holds 32,767 characters. If you assign a longer string to
Range("A1").Value, Excel raises a runtime error rather than truncating. Check length before writing:If Len(myText) > 32767 Thenhandle it separately. - Test the actual limit safely. In the Immediate window (Ctrl+G), run
? Len(String(32767, "a")). It returns 32767. Try 32768 and you will see the assignment or display fail depending on where you send it. This confirms the boundary without corrupting a workbook. - Watch the concatenation trap. Building a string in a loop with
myText = myText & newLineis slow and can hit memory issues with very large results. For long documents, write to a cell or file in chunks, or use a StringBuilder-style approach with an array andJoin. - Handle overflow gracefully. Wrap risky assignments in error handling:
On Error GoTo TooLong, then in the handler log the length withLen(myText)and split the string into 32,000-character pieces before writing to successive cells. - Split long text into cells. Use a loop:
For i = 1 To Len(myText) Step 32000: Cells(r, 1).Value = Mid(myText, i, 32000): r = r + 1: Next i. This keeps each write under the cell cap. - Check other output targets. A MsgBox is practically limited to about 1,024 characters on many systems; a UserForm TextBox has its own MaxLength property (default 0 = unlimited, but performance degrades with huge text). Debug.Print to the Immediate window also truncates long lines.
- Save and test with realistic data. Run your macro on a copy of the workbook first. Confirm that no cell shows a
#VALUE!or that your error handler fires as expected.
Common Mistakes
- Assuming the String type is the limit. It is not — the cell, MsgBox, or TextBox usually fails first.
- Using fixed-length strings by accident.
String * ntruncates silently, which is hard to debug. - Concatenating in a tight loop. Repeated
&on a growing string is slow and can exhaust memory before you hit any documented limit. - Ignoring the 32,767 cell cap. Writing a longer value raises an error, not a warning.
- Trusting MsgBox for long output. It silently cuts off or errors depending on the string.
- Forgetting that Len counts characters, not bytes. Unicode characters still count as one character in VBA's Len, but byte length differs if you convert with StrConv.
Tips
- Use
Len()before every large assignment to a cell or control. - Prefer writing long text to a text file with
Open ... For OutputandPrint #when the content exceeds a cell. - For building large strings, collect pieces in a
Collectionor array and useJoinonce at the end. - Turn on
Option Explicitat the top of every module to catch undeclared variables early. - Keep a helper function like
SafeWrite(cell, text)that splits and writes in chunks — reuse it across projects. - Test on a copy of the workbook, especially when experimenting with boundary lengths.
Troubleshooting
Runtime error when assigning to a cell. The string exceeds 32,767 characters. Split it with Mid and write to multiple cells, or save to a file.
MsgBox shows only part of the text. MsgBox has a practical display limit. Write the full text to a cell or file and show a short summary instead.
Macro slows to a crawl. Likely repeated concatenation. Switch to an array plus Join, or write in chunks.
String appears padded with spaces. You declared a fixed-length string. Change to Dim s As String.
Immediate window cuts off output. Debug.Print truncates long lines; write to a cell or file to inspect the full value.
Error handler never fires. Confirm On Error GoTo is at the top of the procedure and the label exists; also check that you did not reset the error with On Error Resume Next.
Frequently Asked Questions
What is the maximum length of a VBA String variable?
A variable-length String can address roughly 2 billion characters in theory, but Excel's cell limit of 32,767 characters is the practical ceiling for anything you store in a worksheet.
Why does my macro fail when I write a long string to a cell?
Because a cell cannot hold more than 32,767 characters. Split the text across multiple cells or write it to a file.
Does a fixed-length String truncate silently?
Yes. Dim s As String * 10 caps the value at 10 characters and pads shorter values with spaces.
Can a MsgBox display a 5,000-character string?
No. MsgBox has a much smaller practical display limit. Write the full text elsewhere and show a short message.
How do I count characters in a VBA string?
Use Len(myText). It returns the number of characters, including spaces.
Is the limit different on Mac?
The VBA String type and cell limit are the same on Mac Excel; only the editor shortcuts differ.