Excel VBA to Calculate Active Sheet (6 Practical Applications)

Sometimes, when working with large datasets, Excel won’t compute all the formulas in the cells. One solution to this problem is to use VBA to recalculate everything. In this article, we will show how to use Excel VBA to calculate the Active Sheet.

An overview image of VBA Calculate Active Sheet

In the above image, we input a value in cell B2 and inserted a formula in cell C2. Using the ActiveSheet.Calculate method, we obtain the outcomes 10 and 20 in cells B2 and C2 respectively.


What Is VBA Calculate Active Sheet?

The Calculate method in VBA (Visual Basic for Applications) is used to recalculate formulas in a worksheet or a particular range of cells. All the formulas in the active worksheet are calculated when applying the ActiveSheet object’s Calculate function.

Code

Sub Calculate_ActiveSheet()
ActiveSheet.Calculate
End Sub

After defining ActiveSheet.Calculate, this subroutine will compute all the formulas on the current sheet and update the values to correspond with any modifications to the referenced cells when executed.

If you need it, follow this link to learn some different ways to write VBA code in Excel.


Calculate the Active Sheet with Excel VBA: 6 Practical Applications

We will now discuss 6 practical applications using the ActiveSheet.Calculate method in VBA to perform calculations in an active sheet. We’ll use a dataset from a workbook containing 4 worksheets to illustrate our methods.


Method 1 – Calculating a Single Sheet

Re-calculating the sheet1

Here, using the Activesheet.Calculate method, all the formulas in the selected active sheet (Sheet1) will be recalculated when we execute the VBA code.

Code

Sub calculate_on_a_specific_sheet()
ThisWorkbook.ActiveSheet.Calculate
Sheets("Sheet1").Calculate
End Sub

Method 2 – Inserting a Value Using ActiveSheet.Calculate

Inserting value with ActiveSheet.Calculate method

In this case, we select a specific sheet (Main Sheet) and insert the string value “Exceldemy” in cell B2.

Code

Sub insert_value()
ThisWorkbook.ActiveSheet.Calculate
Range("B2") = "Exceldemy"
End Sub

Method 3 – Updating Dependent Values

Getting output based on a dependent cell value

Here, we Insert a value and formula depending on that value, obtaining 20 and 30 in the two consecutive cells B2 and C2.

Code

Sub update_dependent_value()
Range("B2").Value = 10
Range("C2").Formula = "=B2 * 2"
ActiveSheet.Calculate
End Sub

Method 4 – Calculating with Conditions

Using conditionals to get an outcome

Calculations based on logical expression will also be executable when we use the ActiveSheet.Calculate method. Here we consider logic for whether the value of B2 is greater than 100. B2 = 10, so cell C2 remains blank after executing the VBA code.

Code

Sub Calculate_via_conditional()
If Range("B2").Value > 100 Then
ActiveSheet.Calculate
Range("C2").Formula = "=B2 * 2"
End If
End Sub

Method 5 – Finding the Total Number of Rows

Figuring out the total number of rows from active sheet using VBA code

Here we calculate the total number of rows in a range. We select Sheet1, define Rng as a Range (“B2:D11“), and using the Rng.Rows.Count function we obtain Total Rows of 10 in the Immediate window.

Code

Sub Total_rows()
Set Rng = Range("B2:D11")
ActiveSheet.Calculate
Debug.Print "Total Rows: " & Rng.Rows.Count
End Sub

Method 6 – Finding the Total Number of Columns

Figuring out the total number of columns

In the same way as counting rows in a range, we can calculate the total number of columns. We select Sheet1, define Rng as a Range (“B2:D11“), and using the Rng.Columns.Count function we obtain Total Columns of 3 in the Immediate window.

Code

Sub Total_columns()
Set Rng = Range("B2:D11")
ActiveSheet.Calculate
Debug.Print "Total Columns: " & Rng.Columns.Count
End Sub

Excel VBA Manual Calculation on One Sheet Only

Enabling manual calculation on a specific sheet only

Generally, automatic calculation is the default setting when working with formulas in worksheets. To perform a single, manual calculation, we’ll therefore need to turn off automatic calculation.

First, we declare ws as a worksheet and assign to it Sheet1. Using the Application.Calculation= xlCalculationManual statement we turn off automatic calculation. Finally, ws.Calculate recalculates the formulas in Sheet1.

Code

Sub Calculate_manually_in_one_sheet()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Application.Calculation = xlCalculationManual
ws.Calculate
End Sub

Frequently Asked Questions(FAQs)

1. How do you calculate active worksheets?

Utilizing the ActiveSheet.Calculate method, we can select active worksheets and calculate accordingly in them. For example, the following lines insert the text ‘Exceldemy ” in cell B2.

ThisWorkbook.ActiveSheet.Calculate
Range("B2") = "Exceldemy"

2. How do I select an active workbook in Excel VBA?

Use the ActiveSheet.Calculate method in the code and select the required sheet before hitting the Run icon.

Range("B2").Value = 10
ActiveSheet.Calculate

This code inserts 10 in cell B2 of the active workbook.

3. What is the difference between Active Sheet and Selected Sheets in VBA?

One or more worksheets that were specifically chosen within an Excel window are referred to as the Selected Worksheets. There is a specific collection of Selected Worksheets for each Workbook. The worksheet you are observing and working on right now is called the Active Worksheet.


Download Practice Workbook


 

Related Article

Get FREE Advanced Excel Exercises with Solutions!
MD Tanvir Rahman
MD Tanvir Rahman

MD Tanvir Rahman, BUET graduate in Naval Architecture and Marine Engineering, dedicated over a year to the ExcelDemy project. He is an Excel and VBA Content Developer. Having authored 50+ insightful articles, he actively updates and improves over 80 articles, reflecting his commitment to accuracy and currency, managing day-to-day operations, and analyzing and developing Excel and VBA tutorials. His broad interests encompass Data Analysis, Advanced Excel, VBA Macro, Excel Templates, Excel Automation, and Excel Power Query, showcasing a... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo