A variable-length VBA String can hold up to approximately 2 billion characters in theory, but available memory usually becomes the practical limit first. Excel worksheet cells have a separate, much smaller limit: 32,767 characters per cell, so a VBA string can be longer than the text Excel can store in one cell.
What “string limit” can mean
People asking about an “Excel VBA string limit” may mean the maximum size of a VBA variable, the amount of text that fits in a worksheet cell, or the maximum length of a formula. These are different limits imposed by different parts of Office. A string can exist in macro memory without being written to a cell, and a cell limit does not define the capacity of the VBA data type.
For a variable-length VBA string, Microsoft documents a theoretical capacity of approximately 231 characters. That is an upper bound, not a practical promise that a macro can create a string of that size. Your computer must have enough usable memory, and Excel and other running programs also need memory. Large temporary copies can cause an allocation to fail well before the documented ceiling.
- VBA variable: approximately 2 billion characters in theory; actual capacity depends on memory and the operation being performed.
- Worksheet cell: up to 32,767 characters.
- Excel formula: up to 8,192 characters in a formula, separate from the cell’s text-content limit.
How VBA String variables work
A variable-length string is declared with As String. It can grow or shrink as you assign different text to it. VBA stores string data in Unicode form, so the memory used is not simply the number of characters times one byte; internal representation and temporary values also consume space. This is one reason a large string may need substantially more free memory than its visible text suggests.
VBA also supports fixed-length strings, declared with a length after an asterisk. Microsoft documents fixed-length strings as holding from 1 to 65,400 characters. That limit applies to the fixed-length form, not to ordinary variable-length strings. Fixed-length strings are padded with spaces when shorter text is assigned, which can make them inconvenient for general-purpose text processing.
For most macros, use a variable-length string unless you have a specific reason to use a fixed length. A declaration such as Dim message As String does not reserve the theoretical maximum in advance; the string uses memory as its value requires it. Still, repeatedly growing a very large value can create many temporary allocations.
Why an Excel cell has a lower limit
Excel’s worksheet grid has its own content limits. A single cell can contain at most 32,767 characters. This applies whether the text is typed, pasted, or supplied by VBA. Assigning a longer string to a cell cannot make that cell hold the entire value. If the complete text matters, keep it in memory, save it outside the grid, or divide it among multiple cells.
A cell’s display is not a reliable way to judge how much text it contains. Column width, row height, wrapping, and other formatting affect what you can see on the sheet. The formula bar can show more of a cell’s contents, but it does not raise the 32,767-character storage limit.
Formula length is a different restriction: an Excel formula can be no longer than 8,192 characters. A formula that returns text is still subject to the worksheet cell’s 32,767-character content limit. In other words, a short formula can produce a long text result, but it cannot make a cell exceed its content capacity.
Check a string’s length in VBA
Use the VBA Len function to check the length of a string. It is useful for confirming that text is longer than one cell can hold, validating input, or deciding how to split a value. For example, if Len(textValue) returns 40,000, the variable has a 40,000-character value, but the whole value will not fit in one Excel cell.
Do not confuse the number of characters in a VBA variable with the number of cells needed to store it. If you split text into chunks of 32,767 characters, a value of 70,000 characters needs three cells: two full chunks and a final chunk of 4,466 characters. If your text includes special Unicode characters, consider testing the actual values you need to preserve when moving data between programs; different applications and interfaces may handle characters differently.
Put longer text into multiple cells
The following example creates a 70,000-character string and writes it in chunks down column A. It demonstrates that the string can be longer than a cell while each individual cell receives no more than 32,767 characters. Save a copy of the workbook before running any macro: macros cannot be undone with Ctrl+Z.
Sub WriteLongStringInChunks()
Dim textValue As String
Dim startPos As Long
Dim outputRow As Long
Dim chunkLength As Long
' Create sample text longer than one cell can hold.
textValue = String$(70000, "x")
chunkLength = 32767
outputRow = 1
' Write each portion to its own cell in column A.
For startPos = 1 To Len(textValue) Step chunkLength
Cells(outputRow, 1).Value = Mid$(textValue, startPos, chunkLength)
outputRow = outputRow + 1
Next startPos
End Sub
After the macro runs, column A contains three cells of text: 32,767 characters, 32,767 characters, and 4,466 characters. This is a simple storage pattern, not a complete data-management strategy. If you later combine the pieces, join them in the correct order and account for any separators you intentionally added. Also decide how to handle text that begins with characters Excel may interpret specially, such as an equals sign, if your source contains arbitrary user input.
Building and processing very large strings
How you build a string can affect speed and memory use. Repeatedly appending small pieces with an expression such as textValue = textValue & nextPiece may require VBA to create and copy new string values over and over. For a modest number of pieces this is often fine. For a large collection, store pieces in an array and combine them with VBA’s Join function, or process data in batches instead of keeping one enormous value in memory.
Batching is often the more robust choice when reading a large file, assembling report text, or handling many records. Work through a manageable section, write or process it, then release references you no longer need. Avoid making several full copies of a large string—for example, by repeatedly transforming the entire value—because each intermediate result adds to peak memory demand.
When the destination is a text file rather than a worksheet, writing the content to a file avoids the per-cell limit. File encoding matters: plain-text output can use different encodings, and a reader may display characters incorrectly if it expects a different encoding. Choose the format and encoding based on the application that will consume the file, and verify that the receiving system supports the file’s size.
Does 64-bit Excel raise the string limit?
Moving from 32-bit to 64-bit Excel can let a process address more memory, which may help with memory-intensive workbooks and macros. It does not remove Excel’s 32,767-character limit per worksheet cell, and it does not make a string limitless. The documented approximate maximum for the VBA variable-length string type remains a theoretical boundary; available memory and other constraints still matter.
On 32-bit Excel, address-space limitations can make memory pressure especially noticeable in large workloads. On 64-bit Excel, more address space may be available, but a macro can still run out of memory or become slow. If a task routinely requires enormous text values, consider whether the full value needs to be held in one VBA variable at all.
Common misconceptions and problems
- “A cell can hold as much text as a VBA string.” It cannot. The VBA variable and worksheet cell have separate capacities.
- “A 64-bit version removes Excel’s worksheet limits.” It may help with memory availability, but it does not increase the cell character limit.
- “The text is missing because the cell looks too small.” Formatting can hide text from view, but it cannot increase the cell’s maximum stored content. Check the value itself and its length.
- “A formula can be as long as its result.” Formula text has an 8,192-character limit, while a returned cell value has its own limit of 32,767 characters.
If a macro errors while creating or modifying a large string, check how much data is being copied, whether the operation builds the value repeatedly, and whether the destination is a single cell. Test with smaller inputs first, and do not treat a successful small test as proof that the same method will scale to gigabytes of text.
Frequently Asked Questions
What is the maximum length of a VBA String?
A variable-length VBA string is documented as holding approximately 231 characters in theory. In practice, memory availability and the way the macro creates or processes the value usually impose a lower limit.
Can an Excel cell contain more than 32,767 characters?
No. A worksheet cell can contain up to 32,767 characters. Put longer text in multiple cells or store it in a file or another suitable data source.
Does String * 100 have a 100-character maximum?
Yes. A declaration such as Dim code As String * 100 creates a fixed-length string of 100 characters, padded with spaces as needed. This is different from declaring a variable-length string with As String.
Does 64-bit Excel allow longer strings in cells?
No. 64-bit Excel can provide more addressable memory for some workloads, but the worksheet cell limit remains 32,767 characters. It does not make cell contents unlimited.
How can I store a VBA string longer than one cell?
Split it into portions no longer than 32,767 characters and place each portion in a separate cell, or write the text to a file. Keep the chunk order and any separators consistent so you can reconstruct the original text.
Related
- Excel Slicer Search: How to Find Items in a Slicer
- Excel Slicer Tutorial: Add, Format, and Connect Filters
- Excel Slicer Search Box: How to Find Items
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Input Box for Password: Masking and Safer OptionsVBA’s built-in InputBox cannot mask a password: characters appear as the user types. To show dots or asterisks…
- VBA Delete Row Based on Cell Value in ExcelTo delete Excel rows when a cell contains a chosen value, loop through the worksheet from the bottom…
- Excel VBA: Find Text and Delete the Matching Row SafelyTo find specific text in Excel and delete every row containing an exact match, run a VBA macro…
- Excel VBA: Copy an Entire Worksheet into an Array SafelyIn Excel VBA, assign a worksheet range’s Value2 property to a Variant to copy its cell values into…
- Delete the Last Row in Excel VBA SafelyTo delete the last data row of an Excel table, use the table’s ListRows collection: tbl.ListRows(tbl.ListRows.Count).Delete. For a…
- Excel VBA AutoFilter: Copy Visible Results to Another SheetYes. In Excel for Microsoft 365 on Windows, apply AutoFilter to the source range, then copy its visible…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.