

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


VBA underlies the entire Office package. Every Office program can be extended or manipulated using VBA.
The easiest way to access the power of VBA is by recording a macro. A macro is a series of commands that are invoked as a unit. Suppose, for example, that the user needed to import data from an external file, sort the data, apply transformations to the data, and generate a report. The data always looked the same (i.e., the same format and content) and the manipulations and reports always looked the same. And suppose that this had to be repeated several ā or even thousands ā of times. Why do it all manually? Office programs allow the user to ātape the procedureā, that is, record keystrokes and mouse clicks that control the various processes needed. That macro can then be given a name, assigned to shortcut keys or a button, etc., to make it easy to run the entire set of commands as a unit.
To learn how to record and execute macros, see the Help utility for any Office program.
Sometimes, it is difficult or impossible to get the macro recorder to record exactly what you want. You might like to have a macro that is more generic and flexible than what is recorded. You can edit macros using the VB editor (AltāF11). The editor shows the VBA code that was recorded and allows the user to change it. In fact, you can write procedures directly in the editor and never use the macro recorder.
Procedures are complex commands that come in two main flavors: subroutines and functions.
A recorded macro will be a subroutine (Sub). A subroutine behaves as though all of the commands were executed from the user interface (keyboard and mouse). It can do anything that you could do by entering the commands manually.
Functions, on the other hand, behave just like the builtāin functions in Excel (e.g., sin, average, stdev ). An Excel function is called as part of a formula in a cell and can only affect the contents of the calling cell. Userādefined functions (UDFs) can be written to work just like the builtāin functions, but do special or complex tasks.
VBA has many standard functions, but it does not have nearly all of the functions that are available in Excel. VBA functions (e.g., sin ) can be called directly.
new = 4 * sin(radians)
The Excelāspecific functions, however, require a special method. Suppose you want to use the Average function.
Avg = WorksheetFunction.Average(vector)
The āWorksheetFunction.ā Qualifier tells VBA to look in Excelās function library.
VBA gets a lot more complicated than this, but the idea here is just to show that with a little knowledge of VBA, the power of Excel expands greatly.