Using a macro to calculate a worksheet.

AndrewKent

Well-known Member
Joined
Jul 26, 2006
Messages
889
I'm looking to add a form button onto quite a large Excel document that will calculate the active worksheets. I'm using this code but it doesn't appear to be calculating all the worksheets. Can anyone offer an alternative

Code:
Sub CalculateWorkbook()
    Dim wks As Worksheet
    Application.Calculation = xlManual
    For Each wks In ActiveWorkbook.Worksheets
        wks.Calculate
    Next
    Set wks = Nothing
End Sub

Andy
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi Andrew

Be careful using For Each ... Next to calculate the worksheets especially if the exact sequence of worksheet claculation is important (eg if some sheet formulas are dependent on other sheets' formulas). Can you not just hit F9? Or will you have other workbooks open that you don't want to calculate?

Richard
 
Upvote 0
If you are going to calculate one worksheet at a time like that, links between worksheets may not update correctly. For example if Worksheets(1) references Worksheets(2), formulas in the source worksheet won't have been recalculated when the target is recalculated.

Better to use:

Application.Calculate

and let Excel sort out the order.
 
Upvote 0
If you are going to calculate one worksheet at a time like that, links between worksheets may not update correctly. For example if Worksheets(1) references Worksheets(2), formulas in the source worksheet won't have been recalculated when the target is recalculated.

Better to use:

Application.Calculate

and let Excel sort out the order.

Hi Andrew,

Yeah, what you've stated is correct, I have a cover sheet referencing to background sheets that have the calucations on them and it does not update the links. I was hoping to have Excel loop through in a specified order (ie calculate the background sheets then the cover sheet last). When you say Application.Calculate, I thought this is what I already had in my code, how does your suggestion differ from that?

Andy
 
Upvote 0
Andrew

Application.Calculate will cause Excel to recalculate all open workbooks (and all sheets therein), not just the currently active one. You were calculating sheet-by-sheet.

Richard
 
Upvote 0
Ah okay I'm with you now. In the end I simply went through each sheet in the order with this code...

Sub CalculateWorkbook()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

Sheets("Data Extract").Select
Application.Calculation = xlManual
Sheets("Data Extract").Calculate
Sheets("Matrix").Select
Application.Calculation = xlManual
Sheets("Matrix").Calculate
Sheets("Campaign Suite").Select
Application.Calculation = xlManual
Sheets("Campaign Suite").Calculate
End Sub

And it seems to work just as well. Not big, not clever but it gets the job done.

Andy
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,306
Members
448,564
Latest member
ED38

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