Solved

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

Posted on 2013-06-04
4
380 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 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

27 Experts available now in Live!

Get 1:1 Help Now