Faster Code

Braddrum

New Member
Joined
Oct 28, 2008
Messages
12
Can anyone help me speed up this code? It takes way too long to run

Code:
Dim file As Integer, x As Integer, rw As Integer, cl As Integer
    Dim fl As String
    Dim count As Integer
    
    Cells(1, 37).Select

    fl = Dir("C:\(Path)" & "*.xls")
    Do While Len(fl) > 0
        ActiveCell.Formula = fl
        ActiveCell.Offset(1, 0).Select
        fl = Dir()
    Loop
    
    Cells(8, 37) = ""
    x = Cells(Rows.count, 37).End(xlUp).Row
    
    For cl = 1 To 7
    For rw = 2 To 8761    
    For file = 1 To x
        Cells(3, 36) = "='C:\(path)[" & Cells(file, 37) & "]Sheet1'!A" & rw & ""
        Cells(2, 36) = Cells(2, 36) + Cells(3, 36)
    Next file
    
    Cells(rw, cl) = Cells(2, 36) / 7
    Range("AJ2").Clear
        
    Next rw
    
    Next cl

This program averages 7 worksheets from different workbooks cell by cell for column A2:A8761 (hours in a year), but essentially never finishes, I know I could use more ram, but I am afraid it wouldn't help that much, I currently have 2GB, the program usually stops around row 5000, but that is after about 10 minutes. Is there a way to speed this up?

Thanks!
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Have you tried to copy Sheet1, Range A2:A8761 from each of the workbooks to a Summary workbook then perform the averaging there using in-cell formulas?
Workbook 1 to Summary Workbook, Column A
Workbook 2 to Summary Workbook, Column B
...
Workbook 7 to Summary Workbook, Column G
then formulas in column H2:
Code:
=IF(ISERROR(AVERAGE(A2:G2)),"",AVERAGE(A2:G2))
Copied down to H8761
 
Upvote 0
Thanks for the reply

If at all possible I would like to automate the process, in the end i will have to do this average 125 times for all of my various workbooks, and will have to expand this from A2:AH8761, I am just trying to piece this problem together bits at a time

Brad.
 
Upvote 0
I would then copy data from each worksheet, cells A2:AH8761 into a summary workbook (using copy/paste special - values), then use a summary worksheet in the summary workbook to perform the averaging. Turn on the macro recorder and do this with a subset of the data (A2:H20 perhaps) from each worksheet to get a feel for how the code works then expand it to cover the entire range.

Ensure all of the copied data worksheet tabs were between the S and E worksheets then use a formula like this =AVERAGE('(S):(E)'!A2) with the S and E worksheets blank, then the result would be the average of all cell A2 of all the worksheets between the S and E worksheets.

S and E stand for Start and End - can be any valid names.

The other option would be to have the worksheets in the summary workbook permanently linked to the appropriate cells in the input worksheets. When the summary workbook was opened it would pull data from the input worksheets. I am not sure which of those methods would update quicker, but either would be quicker than adding each cell individually for each worksheet.
 
Upvote 0
Thanks again for the help, it did the trick and it is automated/much faster than what I was doing before.

Brad
 
Upvote 0

Forum statistics

Threads
1,215,281
Messages
6,124,045
Members
449,139
Latest member
sramesh1024

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