Microsoft KB Archive/832136

= Data may be truncated when you transfer array data to cells in an Excel worksheet =

Article ID: 832136

Article Last Modified on 2/2/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition

-



SYMPTOMS
When you run a Microsoft Visual Basic for Applications (VBA) macro to transfer data from a VBA array that contains strings of data to a range of cells in a Microsoft Excel worksheet, the data may be truncated (cut off).

Note In Microsoft Office Excel 2003 and in later versions of Excel, you may receive the following error message when you run the VBA macro in the Visual Basic Editor:

Run-time error '1004':

Application-defined or object-defined error

If you run the VBA macro from your Excel worksheet, you may receive the following error message:

Microsoft Visual Basic: 400



CAUSE
This problem may occur when one of the following conditions is true:
 * In Excel 2007, the VBA array is longer than 8,203 characters in length.
 * In Excel 2003 and in earlier versions of Excel, the VBA array is longer than 1,823 characters in length.



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

To work around this problem, populate each cell in your worksheet one at a time from the array, instead of populating the whole range at one time.

To do this, use a VBA macro that is similar to the following example: Sub PopulateRangeWithArray

Dim x

ReDim x(1 To 2, 1 To 2) x(1, 1) = String(2000, &quot;a&quot;): x(1, 2) = String(5000, &quot;b&quot;) x(2, 1) = String(17000, &quot;c&quot;): x(2, 2) = String(33000, &quot;d&quot;) MsgBox Len(x(1, 1)) & &quot;,&quot; & Len(x(1, 2)) _ & &quot;,&quot; & Len(x(2, 1)) & &quot;,&quot; & Len(x(2, 2)) Range(&quot;a1&quot;).Value = x(1, 1) Range(&quot;b1&quot;).Value = x(1, 2) Range(&quot;a2&quot;).Value = x(2, 1) Range(&quot;b2&quot;).Value = x(2, 2)

End Sub

