I have run into an issue in Excel 2007 which is very similar to a known problem of Excel 2003 described by Microsoft Support here ("You may receive a "Run-time error 1004" error message when you programmatically set a large array string to a range in Excel 2003"). The problem reproduces if run the following macro:
Sub newMacro()
Dim longStr As String
longStr = String(8204, "a")
Dim values(3)
For i = 0 To 2
values(i) = longStr
Next i
Range("A1:C1").Value = values
End Sub
Excel gives the "Run-time error '1004'" error message when assigning an array value to a range. But when string length is 8203 characters everything works just fine.
The situation does look strange to me because the "Total number of characters that a cell can contain" limitation for Ecxel 2007 (mentioned in "Excel specifications and limits" here) is 32767.
Additional information:
- The problem doesn't reproduce in Excel 2010.
- The problem doesn't reproduce if set values cell-by-cell without use of an array. But in my case it will dramatically slow down my code.
- Initially I noticed the problem when working with Excel through our Excel add-in that was written in C# using NetOffice libraries.
Did anyone run into this issue? Are there any workarounds for it? Are there any comments by Microsoft on the case? I didn't find anything.
It seems that Microsoft has written a KB Article about this as well: MS KB 832136.
From the article:
Cause: This problem may occur when one of the following conditions is true:
Workaround:
Microsoft suggest that in stead of placing the entire array into you worksheet at once, you should populate the worksheet one at a time from your array. They provide the following sample code in their article as a suggestion on how to do this: