Docsity

Visual Basic for Applications - Computer Methods in Biological Engineering | BAE 200, Study notes of Agricultural engineering

Toggle sidebar
bg1
BAE 200 - Week 14
10/25/08
VBA - Visual Basic for Applications
Dr. Rod Huffman
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)
pf2

Partial preview of the text

Download Visual Basic for Applications - Computer Methods in Biological Engineering | BAE 200 and more Study notes Agricultural engineering in PDF only on Docsity!

BAE 200 - Week 14

VBA - Visual Basic for Applications

Dr. Rod Huffman

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.