VBA: Visual Basic for Applications - Introduction
What is VBA?
VBA is a version of Visual Basic (VB) available in most Microsoft (MS) Office applications such as MS
Excel, MS Word, and MS PowerPoint.
A more limited version of VB, known as VBScript, is available in
MS Access (the MS Office database application), as well as in MS Internet Explorer (the MS web
In MS Office applications VBA is used mainly to develop procedures, known as
can be run within the application itself.
By using VBA within a given application you can use the application's own interface to run a program (a
macro) rather than creating your own interface as you do when developing a VB program from scratch.
For example, when using VBA within MS Excel we can use the Excel spreadsheet cells as input or output
locations for a given numerical procedure.
Other than that, programming a macro within MS Excel, or any
MS Office application, for that matter, is not different than programming a sub procedure in VB.
VB commands, such as Open, Close, Input, Print, If blocks, Do While, Do Until, For…Next, etc., are also
available within VBA.
Numerical solutions in MS Excel
MS Excel is an excellent application for developing numerical solutions.
Even without programming
macros in the spreadsheet, you can solve many numerical problems by using the operations and functions
already available in MS Excel itself.
For instance, the examples on numerical solution of non-linear
equations, numerical integration, and calculations of statistics of a sample, provided in earlier handouts,
were developed using MS Excel.
Considering that spreadsheet programs are commonly available, I expect that you have had a minimum
amount of experience in using MS Excel (or other spreadsheet applications, e.g., Quattro Pro) including the
production of tables, use of pre-defined functions, and creation of graphics.
If you have not used MS Excel
before, I would recommend that you learn its basic operation by following one of the many books on the
subject available in any local bookstore.
Some of those books may be available at the USU library for
My understanding is that those students that took Freshman Engineering seminar at USU have
had experience with MS Excel or Quattro Pro spreadsheets.
Therefore, I will be covering mainly the use of
VBA within an MS Excel spreadsheet, rather than the operation of the spreadsheet per se.
Combining MS Excel and VBA - An Example
As indicated above, MS Excel worksheet cells can be used as input locations for a VBA program.
input cells in the worksheet can be referenced within a VBA program (macro) by using the VBA functions
As an example of a simple VBA application within Excel, open an Excel worksheet and
fill out the cells as shown in the figure below.
The contents of cells A1 and A3 are simply strings that
cover additional cells to the right. These strings are entered by clicking on the corresponding cell and