Vba excel worksheet calculate

title keywords f1_keywords ms.prod api_name ms.assetid ms.date ms.localizationpriority

Worksheet.Calculate method (Excel)

vbaxl10.chm175078

vbaxl10.chm175078

excel

Excel.Worksheet.Calculate

7e807ae0-cd97-d95b-f4c4-af1e5674943e

05/30/2019

medium

Worksheet.Calculate method (Excel)

Calculates all open workbooks, a specific worksheet in a workbook, or a specified range of cells on a worksheet, as shown in the following table.

Syntax

expression.Calculate

expression A variable that represents a Worksheet object.

Remarks

To calculate Follow this example
All open workbooks Application.Calculate (or just Calculate)
A specific worksheet Worksheets(1).Calculate
A specified range Worksheets(1).Rows(2).Calculate

Example

This example calculates the formulas in columns A, B, and C in the used range on Sheet1.

Worksheets("Sheet1").UsedRange.Columns("A:C").Calculate

[!includeSupport and feedback]

Home / VBA / VBA Calculate (Cell, Range, Row, & Workbook)

By default, in Excel, whenever you change a cell value Excel recalculates all the cells that have a calculation dependency on that cell. But when you are using VBA, you have an option to change it to the manual, just like we do in Excel.

calculate-cell-value-using-vba

Using VBA Calculate Method

You can change the calculation to the manual before you start a code, just like the following.

Application.Calculation = xlManual

When you run this code, it changes the calculation to manual.

using-vba-calculate-method

And at the end of the code, you can use the following line of code to switch to the automatic.

Application.Calculation = xlAutomatic
switch-to-automatic-using-vba

You can use calculation in the following way.

calculation-method-vba-code
Sub myMacro()
    Application.Calculation = xlManual   
        'your code goes here   
    Application.Calculation = xlAutomatic
End Sub

Calculate Now (All the Open Workbooks)

If you simply want to re-calculate all the open workbooks, you can use the “Calculate” method just like below.

Calculate

Use Calculate Method for a Sheet

Using the following way, you can re-calculate all the calculations for all the

ActiveSheet.Calculate
Sheets("Sheet1").Calculate

The first line of code re-calculates for the active sheet and the second line does it for the “Sheet1” but you can change the sheet if you want.

Calculate for a Range or a Single Cell

In the same way, you can re-calculate all the calculations for a particular range or a single cell, just like the following.

Sheets("Sheet1").Range("A1:A10").Calculate
Sheets("Sheet1").Range("A1").Calculate

Skip to content

Calculate Method of Worksheet Object VBA

  • VBA Calculate Method Excel Worksheet Object

Calculate Worksheet Method in VBA is used to calculate all open workbooks or a specific worksheet in a workbook or a specified range in a worksheet.

VBA Calculate Method Excel Worksheet Object

  • Why we use Calculate Worksheet Method?
  • VBA Calculate Worksheet Method- Syntax
  • VBA Calculate Worksheet Method: All Open Workbooks
  • VBA Calculate Worksheet Method: In Specific Sheet
  • VBA Calculate Worksheet Method: In Specified Range
  • VBA Calculate Worksheet Method- Instructions

Why we use Calculate Worksheet Method in VBA?

If a workbook or a worksheet or a specific range has formulas we need to refresh each time when the values are changing. In that time we can use Calculate Worksheet Method in VBA.

VBA Calculate Worksheet Method- Syntax

Here is the example syntax to Calculate Worksheet method in Excel VBA.

Worksheets(“YourSheetName”).Calculate

Where Worksheet represents the object and Calculate is the method of worksheet object.

VBA Calculate Worksheet Method: All Open Workbooks

Please see the below VBA code to Calculate all open Workbooks. In this example we are using calculate method of application object.

Sub Calculate_AllOpenWorkbooks()
    Application.Calculate
End Sub

VBA Calculate Worksheet Method: In Specific Sheet

Please see the below VBA code to Calculate in specified Worksheet named “Sheet1”.

Sub Calculate_Sheet()
    Sheets("Sheet1").Calculate
End Sub

VBA Calculate Worksheet Method: In Specified Range

Please see the below VBA code to Calculate in specified range (“A1:E10”) in a specified worksheet named “Sheet1”.

Sub Calculate_Range()
    Sheets("Sheet1").Range("A1:E10").Calculate
End Sub

VBA Calculate Worksheet Method: Instructions

Please follow the below step by step instructions to execute the above mentioned VBA macros or codes:

  1. Open an Excel Worksheet
  2. Press Alt+F11 to Open VBA Editor
  3. Insert a Module from Insert Menu
  4. Copy the above code for activating worksheet and Paste in the code window(VBA Editor)
  5. Save the file as macro enabled Worksheet
  6. Press ‘F5’ to run it or Keep Pressing ‘F8’ to debug the code line by line and observe the calculations in the Worksheet.
Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Excel VBA Project Management Templates
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project Management Template

Ultimate Resource Management Template

Project Portfolio Management Templates
      • In this topic:
  • Why we use Calculate Worksheet Method in VBA?
  • VBA Calculate Worksheet Method- Syntax
  • VBA Calculate Worksheet Method: All Open Workbooks
  • VBA Calculate Worksheet Method: In Specific Sheet
  • VBA Calculate Worksheet Method: In Specified Range
  • VBA Calculate Worksheet Method: Instructions

VBA Reference

Effortlessly
Manage Your Projects

120+ Project Management Templates

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

120+ PM Templates Includes:
By PNRaoLast Updated: March 2, 2023

One Comment

  1. doug wesson
    November 18, 2016 at 3:43 AM — Reply

    Is there a way to call VBA Code when a cell value gets changed, when one calculates the worksheet

Effectively Manage Your
Projects and  Resources

With Our Professional and Premium Project Management Templates!

ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.

We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.

Project Management
Excel VBA

Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.

Analysistabs Logo

Page load link

VBA Projects With Source Code

3 Realtime VBA Projects
with Source Code!

Take Your Projects To The Next Level By Exploring Our Professional Projects

Go to Top

Return to VBA Code Examples

In this Article

  • Calculate Now
  • Calculate Sheet Only
  • Calculate Range
    • Calculate Individual Formula
  • Calculate Workbook
    • Calculate Workbook – Methods That Don’t Work

This tutorial will teach you all of the different Calculate options in VBA.

By default Excel calculates all open workbooks every time a workbook change is made. It does this by following a calculation tree where if cell A1 is changed, it updates all cells that rely on cell A1 and so on.  However, this can cause your VBA code to run extremely slowly, as every time a cell changes, Excel must re-calculate.

To increase your VBA speed, you will often want to disable automatic calculations at the beginning of your procedures:

Application.Calculation = xlManual

and re-enable it at the end:

Application.Calculation = xlAutomatic

However, what if you want to calculate all (or part) of your workbooks within your procedure?  The rest of this tutorial will teach you what to do.

Calculate Now

You can use the Calculate command to re-calculate everything (in all open workbooks):

Calculate

This is usually the best method to use. However, you can also perform more narrow calculations for improved speed.

Calculate Sheet Only

You can also tell VBA to calculate only a specific sheet.

This code will recalculate the active sheet:

ActiveSheet.Calculate

This code will recalculate Sheet1:

Sheets("Sheet1").Calculate

Calculate Range

If you require a more narrow calculation, you can tell VBA to calculate only a range of cells:

Sheets("Sheet1").Range("a1:a10").Calculate

Calculate Individual Formula

This code will calculate only an individual cell formula:

Range("a1").Calculate

Calculate Workbook

There is no VBA option to calculate only an entire workbook. If you need to calculate an entire workbook, the best option is to use the Calculate command:

Calculate

This will calculate all open workbooks.  If you’re really concerned about speed, and want to calculate an entire workbook, you might be able to be more selective about which workbooks are open at one time.

Calculate Workbook – Methods That Don’t Work

There are a couple of methods that you might be tempted to use to force VBA to calculate just a workbook, however none of them will work properly.

This code will loop through each worksheet in the workbook and recalculate the sheets one at a time:

Sub Recalculate_Workbook()
    Dim ws As Worksheet
    
    For Each ws In Worksheets
        ws.Calculate
    Next ws
End Sub

This code will work fine if all of your worksheets are “self-contained”, meaning none of your sheets contain calculations that refer to other sheets.

However, if your worksheets refer to other sheets, your calculations might not update properly.  For example, if you calculate Sheet1 before Sheet2, but Sheet1’s formulas rely on calculations done in Sheet2 then your formulas will not contain the most up-to-date values.

You might also try selecting all sheets at once and calculating the activesheet:

ThisWorkbook.Sheets.Select
ActiveSheet.Calculate

However, this will cause the same issue.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
vba save as

Learn More!

In this tutorial, we’ll discuss the BeforeDelete, Calculate, FollowHyperlink and LensGalleryRenderComplete worksheet events. The Worksheet_BeforeDelete event-handler procedure executes when the worksheet is about to delete. The Worksheet_Calculate event-handler procedure executes after the worksheet is recalculated. The Worksheet_FollowHyperlink event-handler procedure executes when any hyperlink is clicked on the worksheet. The Worksheet_LensGalleryRenderComplete procedure executes when the user selects the Quick Analysis tool.

The worksheet event-handler procedures must be in the code module for that worksheet. Put them somewhere else, and they won’t work. You can quickly access that code window by right-clicking the worksheet’s tab and selecting the View Code:

Worksheet view code window

Worksheet_BeforeDelete

The BeforeDelete  event occurs when the worksheet is about to be deleted. The Worksheet_BeforeDelete event procedure does not have a Cancel argument, so it is not possible to prevent the worksheet from being deleted.

Private Sub Worksheet_BeforeDelete()
 MsgBox ActiveSheet.Name
End Sub

It will print the current worksheet name before deleting the worksheet.

Worksheet_Calculate

This event occurs after the worksheet is recalculated. Lets try a basic calculations by entering 2 in cell A1 and 3 in cell A2. Enter the formula =A1+A2 in cell A3. Next, open the VBE and write the following code:

Private Sub Worksheet_Calculate()
 MsgBox "Recalculated"
End Sub

Switch to the Excel window and modify the entry in cell A1 or A2 on the sheet. Notice that after leaving Edit mode, the Worksheet_Calculate event procedure is triggered and you are presented with a Recalculated message.

Worksheet_FollowHyperlink

The FollowHyperlink event occurs when you click any hyperlink on the worksheet. The Worksheet_FollowHyperlink procedure accepts Target (the Hyperlink object) as the parameter which represents the destination of the hyperlink. In the following example, we retrieve the address of the cell next to the clicked hyperlink and increment the value in that cell by adding value 1:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
 Dim c As Range
 Set c = Target.Range.Offset(0, 1)
 c.Value = c.Value + 1
End Sub

Worksheet_LensGalleryRenderComplete

Worksheet_LensGalleryRenderComplete occurs when the user selects the Quick Analysis tool.

Private Sub Worksheet_LensGalleryRenderComplete()
 MsgBox "Render complete"
End Sub

by updated Apr 14, 2020

Like this post? Please share to your friends:
  • Vba excel адреса выделенных ячеек
  • Vba excel with outlook
  • Vba excel userform toolbox
  • Vba excel workbooks open параметры
  • Vba excel адрес ячейки в диапазоне