Writing array to excel is slow despite only 2500 rows of data.

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
The following sub procedure is one of a few located in my module.


Code:
Sub writetrans()

Sheet3.Activate

    For A = 2 To UBound(MainArray2)
    
        Cells(A, 1) = MainArray2(A, 1)      '<-- Date
        Cells(A, 2) = MainArray2(A, 2)      '<-- Description
        Cells(A, 3) = MainArray2(A, 8)      '<-- Updated Amount
            
                Cells(A, 4) = -MainArray2(A, 9)     '<--Running Balance          
                          
        Cells(A, 5) = MainArray2(A, 10)     '<--Updated Category
        Cells(A, 6) = MainArray2(A, 7)      '<--Accounts
    
    Next A
    
End Sub

The procedure is pretty simple. It writes the elements of an array onto an excel sheet. However, When I run this procedure it is extremely slow. For isntance, I get the " not responding" message whenever I run the procedure. Yet, when I escape from the procedure I can see some rows have been written on the excel sheet. I only have 2500 rows of data it shouldn't be this slow. Also I didn't have this issue when I tested the code a couple of days ago.

What could be causing this issue?

Also I turned off screen updating and auto calculations.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Reading/writing to the sheet is one of the slowest things you can do in VBA. It's far faster to write the entire array in one shot.

In your example, you are rearranging the order of the output columns. What you'd need to do is create an array with the columns in the output order (or just reorder the columns in your existing array). Something like:

Code:
    ReDim MainArray3(1 to UBound(MainArray2) - 1, 1 to 6)
    For A = 2 to UBound(MainArray2)
        MainArray3(A - 1, 1) = MainArray2(A, 1)
        MainArray3(A - 1, 2) = MainArray2(A, 2)
        MainArray3(A - 1, 3) = MainArray2(A, 8)
        MainArray3(A - 1, 4) = -MainArray2(A, 9)
        MainArray3(A - 1, 5) = MainArray2(A, 10)
        MainArray3(A - 1, 6) = MainArray2(A, 7)
    Next A

     Sheet3.Cells(2, 1).Resize(UBound(MainArray3), 6).Value = MainArray3

This should run very, very fast. Let us know if this works for you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top