?
Solved

What is the quickest way to write a 2-D array to EXCEL?

Posted on 2003-02-22
16
Medium Priority
?
181 Views
Last Modified: 2010-05-01
I am writing a 2-D array to an Excel spreadsheet by the following:

'Write data (number of entries per line = intWord
'and number of lines = intNpoints)
For i = 1 To intNpoints
    For j = 1 To intWord
        .Cells(i + 3, j) = dblData2(i, j)
    Next j
Next i

When intWord = 37 and intNpoints = 41, it takes about 4 seconds on a 900 MHz computer.   I know that VC++ can do it in a fraction of a second, but I am not a VC++ programmer.  Is there a way to do it in VB 6 more quickly than the above code?

Thanks.  
0
Comment
Question by:glenisle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +3
16 Comments
 
LVL 12

Expert Comment

by:guidway
ID: 8001444
I don't think so. Unless you maybe assign dblData(i,j) to a variable above this statement and then assign .Cells(i+3,j) to that variable. I know in Java that would help a little because it was not having to do nested calls (or something like that). Just my 2 cents though. Not sure if it will help in VB. :0)

guidway
0
 
LVL 2

Expert Comment

by:NPluis
ID: 8002446
If I run the following code on a 800 Mhz it runs in less than 1 second:
Dim intNpoints As Integer
Dim intWord As Integer
Dim dblData2() As Double

intNpoints = 41
intWord = 37

ReDim dblData2(intNpoints, intWord)
For i = 1 To intNpoints
    For j = 1 To intWord
        dblData2(i, j) = Round(Rnd(), 3) * 100
    Next j
Next i

With Excel.Sheets("Sheet1")
    For i = 1 To intNpoints
       For j = 1 To intWord
           .Cells(i + 3, j) = dblData2(i, j)
       Next j
    Next i
End With

are you sure the code u placed takes 4 seconds?
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8002788
Here is one thing you can do to speed up the code

Dim max as integer
max = intNpoints + 3
For i = 4 To max
   For j = 1 To intWord
       .Cells(i , j) = dblData2(i, j)
   Next j
Next i

ELiminate the repeated additions inside the loop.

mlmcc
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:vware
ID: 8004527
dim your counter(s) as Long - it'll get you some speed improvement
0
 

Author Comment

by:glenisle
ID: 8005128
NPluis:

I timed it more accurately on my 900 MHz computer and it takes just under 3 sec to do only the write loop:
   For i = 1 To intNpoints
      For j = 1 To intWord
          .Cells(i + 3, j) = dblData2(i, j)
      Next j
   Next i

With one exception, I am using essentially the same coding as you used (including the Redim dblData2(intNpoints, intWord)), so it is still a mystery why yours takes under 1 sec.

The thing I don't use is your first loop:
For i = 1 To intNpoints
   For j = 1 To intWord
       dblData2(i, j) = Round(Rnd(), 3) * 100
   Next j
Next i
0
 

Author Comment

by:glenisle
ID: 8005138
NPluis:

My previous comment was accidentally sent before I finished.  Regarding your first loop, it garbles the array quite drastically and I don't know why you even used it.

Thanks,
glenisle
0
 

Author Comment

by:glenisle
ID: 8005145
mlmcc and vware:

I find that your two suggestions had negligible effect on the run time.

glenisle
0
 
LVL 1

Expert Comment

by:vware
ID: 8005187
activate optimization for pentium pro and try it when you run the compiled exe
0
 

Author Comment

by:glenisle
ID: 8005357
mlmcc and vware:

I find that your two suggestions had negligible effect on the run time.

glenisle
0
 

Author Comment

by:glenisle
ID: 8005382
vware:

Running the compiled code (compiled with activatation of optimization for Pentium Pro checked) had no measurable effect on the run time.

glenisle
0
 
LVL 2

Expert Comment

by:NPluis
ID: 8006772
The first loop was only used to fill the array so I had something to test it with. I haven'tgot a clue why yours takes 3 times as long as mine.
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8012435
Are you actually compiling to an exe and running the exe or are you running from the IDE and compiling as you go?

mlmcc
0
 

Author Comment

by:glenisle
ID: 8014261
mlmcc:

Yes, I am actually compiling to an exe and running it to get the timing.

glenisle
0
 

Expert Comment

by:SpideyMod
ID: 8116573
A request for refund has been made at:
http://www.experts-exchange.com/Community_Support/Q_20547206.html

I'm copying the text so that if I do Refund and PAQ, the information will be here:

The method is described in detail in:
    http://www.vb-helper.com/HowTo/excel.zip
Using that method I could write my 37 x 41 array to an Excel spreadsheet in a blink of a second, instead of 4 seconds, with no other changes to my program.


Experts, please leave any objections within the next 72 hours.  Without them I intend to honor the request.

SpideyMod
Community Support Moderator @Experts Exchange
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 8116728
In his original example, he is opening excel copying the data to a live spreadsheet and able to view when it is done.

In his final answer he is creating an Excel compatible file (it is not a .xls file).  If he now needs to view it, he will have to open Excel then open and convert the file.

mlmcc
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8143672
PAQ'd and points refunded.

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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