Basics of VBA / Macros Chapter – 68 (Application Object Part-1)

Basics of VBA / Macros Chapter – 68 (Application Object Part-1) 1
⏱ 4 min read

This practical VBA exercise explains one small technique at a time. Work in a copy of your workbook, use Alt + F11 to open the Visual Basic Editor, and test the macro on a small sample before running it on live data.

In this article
  1. Before you run the code
  2. What this exercise covers
  3. Test, adapt and continue
  4. Continue learning

Before you run the code

  • Save a macro-enabled copy of the workbook (.xlsm).
  • Use Option Explicit in a standard module to catch misspelled variable names.
  • Read the comments and adjust sheet names, ranges and examples to fit your own file.

What this exercise covers

The mother of all objects is Excel itself. We call it theΒ Application object. The application object gives access to a lot of Excel related options.

Advertisement

WorksheetFunction

You can use the WorksheetFunction property inΒ Excel VBAΒ to access Excel functions.

1. For example, place aΒ command buttonΒ on your worksheet and add the following code line:

Range(“A3”).Value = Application.WorksheetFunction.Average(Range(“A1:A2”))

When you click the command button on the worksheet, Excel VBA calculates the average of the values in cell A1 and cell A2 and places the result into cell A3.

WorksheetFunction in Excel VBA

Instead of Application.WorksheetFunction.Average, simply use WorksheetFunction.Average. If you look at the formula bar, you can see that the formula itself is not inserted into cell A3. To insert the formula itself into cell A3, use the following code line:

Range(“A3”).Value = “=AVERAGE(A1:A2)”

ScreenUpdating

Sometimes you may find it useful to disable screen updating (to avoid flickering) while executing code. As a result, your code will run faster.

1. For example, place aΒ command buttonΒ on your worksheet and add the following code lines:

DimΒ iΒ AsΒ Integer

ForΒ i = 1Β ToΒ 10000
Range(“A1”).Value = i
NextΒ i

When you click the command button on the worksheet, Excel VBA displays each value a tiny fraction of a second and this can take some time.

ScreenUpdating

2. To speed up the process, update the code as follows.

DimΒ iΒ AsΒ Integer

Application.ScreenUpdating =Β False

ForΒ i = 1Β ToΒ 10000
Range(“A1”).Value = i
NextΒ i

Application.ScreenUpdating =Β True

As a result, your code will run much faster and you will only see the end result (10000).

DisplayAlerts

You can instruct Excel VBA not to display alerts while executing code.

1. For example, place aΒ command buttonΒ on your worksheet and add the following code line:

ActiveWorkbook.Close

When you click the command button on the worksheet, Excel VBA closes your Excel file and asks you to save the changes you made.

DisplayAlerts

2. To instruct Excel VBA not to display this alert while executing code, update the code as follows.

Application.DisplayAlerts =Β False

ActiveWorkbook.Close

Application.DisplayAlerts =Β True

As a result, Excel VBA closes your Excel file, without asking you to save the changes you made. Any changes are lost.

Calculation

By default,Β  calculation is set to automatic. As a result, Excel recalculates the workbook automatically each time a value affecting a formula changes. If your workbook contains many complex formulas, you can speed up your macro by setting calculation to manual.

1. For example, place aΒ command buttonΒ on your worksheet and add the following code line:

Application.Calculation = xlCalculationManual

When you click the command button on the worksheet, Excel VBA sets calculation to manual.

2. You can verify this by clicking on File, Options, Formulas.

Calculation Options

3. Now when you change the value of cell A1, the value of cell B1 is not recalculated.

Manual Calculation

You can manually recalculate the workbook by pressing F9.

4. In most situations, you will set calculation to automatic again at the end of your code. Simply add the following code line to achieve this.

Application.Calculation = xlCalculationAutomatic

Test, adapt and continue

Run this example on a few known cells first and compare the result with a manual check. Once it works, replace hard-coded references with named ranges or clear input cells so the macro is safer to reuse.

This post is part of the Excel VBA learning series. For formula-led alternatives and related spreadsheet techniques, see our Excel guides.

⚑ Interactive Practice Lab & Formula Tester:

Test formulas dynamically in our web formula laboratory or download the companion Excel workbook.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong