Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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

↑ Back to the top


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.

↑ Back to the top


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, "a"): x(1, 2) = String(5000, "b")
   x(2, 1) = String(17000, "c"): x(2, 2) = String(33000, "d")
   MsgBox Len(x(1, 1)) & "," & Len(x(1, 2)) _
      & "," & Len(x(2, 1)) & "," & Len(x(2, 2))
   Range("a1").Value = x(1, 1)
   Range("b1").Value = x(1, 2)
   Range("a2").Value = x(2, 1)
   Range("b2").Value = x(2, 2)

End Sub

↑ Back to the top


References

For more information, click the following article number to view the article in the Microsoft Knowledge Base:
166342 Description of the limitations for working with arrays in Excel

↑ Back to the top


Keywords: KB832136, kbprb, kberrmsg, kbvba, kbautomation, kbmacro, kbprogramming

↑ Back to the top

Article Info
Article ID : 832136
Revision : 5
Created on : 2/2/2007
Published on : 2/2/2007
Exists online : False
Views : 362