Solved

How to write a single dimension array into an excel column quickly using VBA

Posted on 2013-06-04
4
379 Views
Last Modified: 2013-06-05
Hi Experts, I think this an easy one, but I'm having trouble figuring it out... I have a one dimension array with about 500,000 values that I need to dump into an excel column quickly.  I'm currently using the code:
For x = 1 To UBound(N_Data)
        Cells(x + 1, Last_Col) = N_Data(x)
Next x

Open in new window

Unfortunately this is very slow.  When I try to simply select the range and set the value equal to my data i.e.:
Range(Cells(2,Last_Col),Cells(Ubound(N_Data)+2),Last_Col)) = N_Data

Open in new window

it dumps blank values.  I think this is because the values in my array need to be transposed...
0
Comment
Question by:JamesCbury
  • 2
4 Comments
 
LVL 15

Assisted Solution

by:ChloesDad
ChloesDad earned 100 total points
ID: 39221234
You need to transpose the array

Dim xdata(9) As Integer

xdata(5) = 5

Range("a1:a10") = Application.Transpose(xdata)

puts a column of zeroes with row 6 = 5 (array is zero based)
0
 

Author Comment

by:JamesCbury
ID: 39222153
Thanks, that seems like it should work, but when I try to apply it I keep getting a Run time error "13" Type Mismatch...  any thoughts?

Range(Cells(2, Last_Col), Cells(UBound(N_Data) + 1, Last_Col)) = Application.Transpose(N_Data)

Open in new window

0
 
LVL 45

Accepted Solution

by:
aikimark earned 300 total points
ID: 39222318
See my Fast Data Push to Excel article:
http://www.experts-exchange.com/A_2253.html

You need to create a 2D array, not a 1D (vector) data structure.
0
 

Author Comment

by:JamesCbury
ID: 39222669
Perfect - that did the trick.  I added a dimension to my array
ReDim N_Data(UBound(R_Data), 1)

Open in new window

Loaded my data into the "0" dimension (starting in the "0" position)
For x = 1 To UBound(R_Data, 1)
     N_Data(x - 1, 0) = 'insert calculated value here
Next x

Open in new window

Then bulk-wrote the results
Last_Col = Cells(1, 100).End(xlToLeft).Column
Range(Cells(2, Last_Col), Cells(UBound(N_Data, 1) + 1, Last_Col)) = N_Data

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now