Excel VBA Calculate Workbook (All/ Single/ Active Workbook)

Here’s an overview of how the entire workbook can be calculated with VBA and the dataset we’ll use to demonstrate various functions.

Total Price Updated


Excel Application.Calculate method

Syntax Function
Application.Calculate Calculates all open workbooks
ActiveSheet.Calculate Calculates active worksheet
Sheets(“Sheet 1”).Calculate Calculates sheet 1
Worksheets(1).Rows(1).Calculate Calculates row 1 of sheet 1
Selection.Calculate Calculates selected cells

How to Calculate Workbooks Using Excel VBA

Example 1 – Calculate All Open Workbooks

We want the Total Price to be updated when we change the Quantity in the following dataset.

Sample Dataset

When the Quantities are changed, the Total Prices are still the same. To update the Total Price, we will use VBA code.

Total Price Not Changing

  • Go to the Developer tab and select Visual Basic to open Microsoft Visual Basic for Applications.

Open Microsoft VBA

  • From the Insert tab, select Module.

Insert Module

  • Insert the following code in the module.
Sub calculateallworkbooks()
Calculate
End Sub

Code to calculate All Open Workbooks

  • Click on the Run button to execute the code.

Run the Code

  • Go to all the open workbooks and you will find that all the calculations are updated based on modified data.

Total Price Updated


Example 2 – Calculate the Active Worksheet

The Total Price column is not updated when the quantity column is changed.

Total Price Not Updated

  • In the VBA code module, insert the following code to calculate only the active worksheet.
Sub activeworksheetcalculate()
ActiveSheet.Calculate
End Sub

VBA Code to Calculate Active Sheet

  • The Total Price column is updated.

Total Price Calculated


Example 3 – VBA to Calculate a Specific Worksheet

  • Use the following VBA code.
Sub specificsheetcalculate()
Sheets("Sheet 3").Calculate
End Sub

VBA Code to Calculate Specific Worksheet

  • Run the code to calculate the Total Price column of Sheet 3.

Total Price Updated


Example 4 – Calculate a Range Using VBA

  • Copy the following code and paste it into the VBA module to calculate a specific range.
Sub calculaterange()
Sheets("Range").Range("E5:E9").Calculate
End Sub

VBA Code to Calculate Range

  • Press the Run button. The range E5:E9 will be calculated.

Range Calculated

Read More: Excel VBA: Create a New Workbook and Name It


Example 5 – Calculate a Single Cell

  • Use the code given below.
Sub calculatecell()
Sheets("cell").Range("E5").Calculate
End Sub

VBA Code to Calculate a Single Cell

  • The cell E5 will be re-calculated.

Single Cell Calculated


Example 6 – Calculate Selected Cells

  • Select the cells you want to force-calculate.

Select cells

  • Apply the following VBA code in the code module and run it.
Sub calculateselectedcells()
Selection.Calculate
End Sub

VBA Code to Calculate selected Cells

  • The selected cells will be updated.

Selected Cells Calculated


Things to Remember

  • You can keep the Calculation Options Automatic for automatic formula calculations.
  • To get correct results, make sure the formulas are correct.

Frequently Asked Questions

What is Ctrl+Alt+F9 in VBA?

If you press Ctrl+Alt+F9, it will calculate all the formulas of each worksheet of all open workbooks.

Is VBA faster than formulas?

It is better to use formulas when the calculation is simple as it is faster. However, if the formula is really complex, it might be better to run a VBA.

How do I prevent automatic calculation in Excel?

Go to the Formulas tab and click on Calculation Options. From there select Manual. Now if you try to change any value in your worksheet, the formulas won’t be calculated automatically.


Download the Practice Workbook

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Alif Bin Hussain
Alif Bin Hussain

Alif Bin Hussain earned a BSc in Civil Engineering from Bangladesh University of Engineering and Technology. As an engineering graduate, he has a deep passion for research and innovation. He loves to play with Excel. In his role as an Excel & VBA Content Developer at ExcelDemy, he not only solves tricky problems but also shows enthusiasm and expertise in handling tough situations with finesse, emphasizing his dedication to delivering top-notch content. He is interested in C, C++,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo