Contents

## The benefits of using Excel for calculation

We are well aware that in today’s world, a machine’s brain works orders of magnitude faster than a human’s brain. By this, I mean that when there is a requirement to calculate some numbers mathematically, we have quickly defaulted to taking out our calculators, mobile phones, and laptops to guarantee quick and accurate results. This is the great boon of the science and technology.

Microsoft Excel is a host application that makes maintenance of documents easy using formulas. For example, formulas can be put to great use with applications like:

• Loan calculation
• Budgeting
• Business relationship documents
• Grade sheet maintenance
• Database maintenance
• Savings calculation

And those formulas can be reused, over and over again. In Excel, if you select a formula in one cell and drag it down, it can easily be copied to other cells and the result is calculated accordingly in each cell.

## How a formula is calculated

There is a category named “Calculations” under the “Formulas” menu. It offers three modes of calculation options:

1. Automatic
2. Automatic except for data tables
3. Manual

These options help us decide when and how the calculation of formulas should be carried out in cells.

### Automatic

By default, all calculations are automatic. For example, let us assume that cell A1 has a value of 10 and cell B1 has a value of 5. Now, in cell C1, if I enter a formula to add A1 and B1, and press enter, “15” will immediately –automatically — be visible in the cell C1.

This means that we do not need to manually invoke the calculation. It happens as soon as the cursor leaves the cell.

### Automatic except for data tables

This option enables Excel to calculate all cells automatically except the ones involving data tables. Here data tables do not mean the normal tables, or the ones loaded through external connections explicitly. It is specifically the Data What-if Analysis Data Table.

### Manual

Let us assume we have the same scenario above where cell A1 as a value of 5 and B1 has a value of 10 . Cell C1 is the sum of A1 and B1 ( `=sum( A1, B1)` ).

Since the default option is “Automatic,” we can see the value of C1 is 15. Now, change the calculation option to “Manual.” Change the value of A1 to 30 and click on enter. You will see that there is no change in the value of cell C1.

Manually, click on the “calculate now” option in the same menu “Formulas & Calculation.” Now we can see that the value of cell C1 has changed to 40.

## Using VBA to turn off or turn on Automation Calculations

The code to set or change the calculation options in VBA is very simple and uses application.calculation.

Syntax

`Application.Calculation= <mode>`

Where the mode can be one of the three below options

• xlCalculationManual
• xlCalculationAutomatic
• xlCalculationSemiAutomatic

### Examples

#### Addition of 2 numbers

Just like above, we’ll add two numbers together in this example. Here we switch the calculation mode to Manual and then again to Automatic to see the difference.

The sum’s value is printed, and a screenshot of the run is also provided for reference.

```Sub calculate_demo()

'Initiating values in 2 cells and setting a formula in the 3rd cell

Cells(1, 1).Value = "5"
Cells(1, 2).Value = "10"
Range("C1").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-2],RC[-1])"

' print the value of the third cell before changing the calculation options

Debug.Print Cells(1, 3).Value

'Change the calculation option to "Manual" and print the 3rd cell value.
'There would be no change

Application.Calculation = xlCalculationManual
Cells(1, 1).Value = "10"
Debug.Print Cells(1, 3).Value

'Use the option to calculate explicitly and try printing again.
'Value of 3rd cell would have got re-calculated

Application.Calculate
Debug.Print Cells(1, 3).Value

'Change the calculation mode to Automatic again.
'As soon as the values of cells change the 3rd cell ( with formula ) value also changes

Application.Calculation = xlCalculationAutomatic
Cells(1, 1).Value = "3"
Debug.Print Cells(1, 3).Value

End Sub
```

#### Validate if a mark is a pass or fail grade

In this example, the first cell is a number and the second cell holds an “if” formula. The value is “Pass” if the value of first cell is greater than 49. If not, the value remains “Fail.”

```Sub calculate_demo()

'Initiating values in 2 cells and setting a formula in the 3rd cell

Cells(1, 1).Value = "5"
Cells(1, 2).Value = "10"
Range("C1").Select
ActiveCell.FormulaR1C1 = "=SUM(RC[-2],RC[-1])"

' print the value of the third cell before changing the calculation options

Debug.Print Cells(1, 3).Value

'Change the calculation option to "Manual" and print the 3rd cell value.
'There would be no change

Application.Calculation = xlCalculationManual
Cells(1, 1).Value = "10"
Debug.Print Cells(1, 3).Value

'Use the option to calculate explicitly and try printing again.
'Value of 3rd cell would have got re-calculated

Application.Calculate
Debug.Print Cells(1, 3).Value

'Change the calculation mode to Automatic again.
'As soon as the values of cells change the 3rd cell ( with formula ) value also changes

Application.Calculation = xlCalculationAutomatic
Cells(1, 1).Value = "3"
Debug.Print Cells(1, 3).Value

End Sub
```

#### Concatenate first name and last name to get the full name

This example sets first name, last name, and then uses a formula to concatenate and display the full name. Again, we toggle between the two calculation options to understand its working.

```Sub calculate_demo()

'Initiating value in the 1st cell and setting a formula in the 2nd cell

Cells(1, 1).Value = "57"
Range("B1").Select
ActiveCell.FormulaR1C1 = "=if(RC[-1]>49,""Pass"",""Fail"")"

' print the value of the second cell before changing the calculation options

Debug.Print Cells(1, 2).Value

'Change the calculation option to "Manual" and print the 2nd cell value.
'There would be no change

Application.Calculation = xlCalculationManual
Cells(1, 1).Value = "10"
Debug.Print Cells(1, 2).Value

'Use the option to calculate explicitly and try printing again.
'Value of 2nd cell would have got re-calculated

Application.Calculate
Debug.Print Cells(1, 2).Value

'Change the calculation mode to Automatic again.
'As soon as the value of cell changes, the 2nd cell ( with formula ) value also changes

Application.Calculation = xlCalculationAutomatic
Cells(1, 1).Value = "86"
Debug.Print Cells(1, 2).Value

End Sub
```

## Conclusion

Although the default calculation option of “Automatic” is simple and easy to use, sometimes due to a huge size or volume of data in any Excel workbook, its performance might slow down. In order to handle this situation, we can switch to “Manual” or the “Semi-Automatic” i.e. the “Automatic except for data tables” options to perform calculations only as required.

We may explicitly use the  “calculate now” and “calculate sheet” options in our code depending on the area of recalculation. Hence these options are available to users in order to improve performance.