Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an array in VB and want to copy the whole array to an excel spreadsheet rather than doing it a line at a time. This will make the process much quicker.

The array code at VB end is like as follows

VB
xlWorkSheet.Range("A2").Resize(x, y).Value = MyArray


The excel spreadsheet has headings

Name, Dept, Salary

Rather than being dispalyed as
Name     Dept        Salary
Smith    Accounts    10000
Green    Sales       14500 
Brown    Accounts    12000


It is getting displayed as
Name       Dept   Salary
Smith      Green  Brown
Accounts   Sales  Accounts
10000      14500  12000


There must be a simple solution?
Posted
Updated 26-Jul-13 1:49am
v2
Comments
Maciej Los 26-Jul-13 7:50am    
And the declaration of MyArray is...
Central_IT 26-Jul-13 8:21am    
MyArray is made up as follows 3 elements and say 1000 records

i.e. MyArray(3,1000)

1 solution

What you've got should work, assuming that A2 is your starting cell, and that x and y are the lengths of your array dimensions. More generically,
VB
xlWorkSheet.Range("A2").Resize(MyArray.GetUpperBound(0) + 1, MyArray.GetUpperBound(1) + 1).Value = MyArray


Be aware that there is (or was) an unadvertised limit on the cell size when populating via an array. Used to be that if your data was more than 912 characters, it'd get left blank. Way to handle that was to check your array for such instances and replace them with some dummy value, storing the indices, and then after you've loaded the sheet, go back through and replace the dummy values. That was an older version of Excel (maybe 2007?), so that may be fixed now.

Looking at your results, I'm guessing your array is loaded wrong. Take a look at the data in the array before your put it in the sheet.
 
Share this answer
 
v2
Comments
Central_IT 26-Jul-13 9:46am    
Thanks for getting back to me. Unfortunately I get the error "Exception from HRESULT: 0x800A03EC" when I try and run this part of the code.

The array is as xlWorkSheet.Range("A2").Resize(30, 4690).Value = MyArray

Do you think this is an excel error?
woopsydoozy 26-Jul-13 9:57am    
Yes, that's an Excel error. But you weren't getting that before, so what's changed? From a quick search, that seems to come up when people reference a cell with 0 as an index--need to use 1 as the base instead.
woopsydoozy 26-Jul-13 11:10am    
Also, see 2nd solution for http://www.codeproject.com/Questions/606812/transferplusanplusarrayplusofplusdataplustoplusapl

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900