Visual Basic For Applications
Visual Basic For Applications
htm#Introduction
1 of 5 8/14/2008 11:04 PM
http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction
the VB IDE.
3. The VBA IDE provides a project navigation tree window (labeled
Project - VBA Project) that lists all Excel/VBA projects currently
open. This window may include a number of
password-protected files which are VBA projects loaded with
Excel -- e.g., Adobe Acrobat writer -- and which cannot be
modified by the user. Your current project will be listed, by
default, as VBA Project(Book1). If you save it with other name,
the change will be reflected in the navigation tree window.
[VBA IDE Figure].
4. Under your current project (i.e., your current Excel workbook),
you will find a list of worksheets (by default there will be three
worksheets named Sheet1, Sheet2, Sheet3) followed by the
workbook itself (default name, This Workbook). Also, a branch
of the file tree may link to a Modules folder with at least one
module (default name, Module1). If no Modules folder is
available, and you want to add a module to your project
(workbook), use the Insert>Module option in the VBA IDE.
[Module Added Figure].
5. A Macro is a VBA program that can be either recorded by the
user or typed as VB code.
6. To record a macro use the option Tools>Macro>Record new
macro... Any action you take between pressing the OK button
and stopping the recording will be sent to a VBA program within
the first VB Module in your project. If no modules are available
at this point, Module1 will be added to the project.
7. To see the macros available in your current project (workbook),
use the option Tools>Macro>Macros. This will produce a form
listing all macros (if any) available to your workbook. [Macros
window].
8. To run a macro, after opening the form listing the available
macros, click on the macro name and press the Run button.
9. To edit a macro, after opening the form listing the available
macros, click on the Edit button. This will take you to the
location of the macro within the VB IDE. The object to which
the macro is attached (i.e., workbook, worksheet, form, or
module) will be highlighted in the VB IDE's navigation tree
window.
10. The Excel control toolbox is useful for adding command buttons
and other VB controls to your spreadsheet. Do a right click on
any empty space on the Excel top tool bar and select the Control
Toolbox. [Excel control toolbox].
11. The Control Toolbox contains a Design Mode toggle button, a
Properties button, and a Code button, as well as a number of
controls (check button, command button, list box, combo box,
2 of 5 8/14/2008 11:04 PM
http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction
a = Range("B2").Value
a = Cells(2,2).Value
17. To place data from your VBA code to the worksheet use the
same functions Range and Cells, e.g.,
Range("C2").Value = r1
Cells(3,2).Value = r1
18. To place string data from your VBA code to the worksheet use,
for example:
3 of 5 8/14/2008 11:04 PM
http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction
Visual Basic, e.g., ACOS, ASIN, BESSELI, etc. You can use the
cells in the worksheet to evaluate formulas that use these
functions and then bring those values into your VBA code.
22. To see a list of the functions available in Excel, click on a cell in
a worksheet, place and equal sign in it and then click on the
button labeled fx in the Excel toolbar. A listing of the functions
by categories will be provided. The categories include All, Date
& Time, Math & Trig, Statistical, etc. Click on any category to
get a listing of functions available. [Functions list window]
23. There is a category of functions referred to as Engineering
functions that include advanced engineering mathematical
functions, e.g., Bessel functions, complex number conversion,
error function, etc. If that category is not available in your
Excel functions, use the option Tools>Add Ins... and mark the
boxes Analysis Toolpack and Analysis Toolpack - VBA to add the
engineering functions. Press OK to load the functions.
24. To label Excel cells with variable names, use the option Insert >
Name > Define after selecting the cell (or cells) of interest.
Type a name for the cell and that name will serve as the cell
identifier. This identifiers are only available for operations
within Excel (e.g., formulas defined in cells). The identifier's
names are not passed on to VBA code.
25. Advantages and disadvantages of using Excel-VBA over Visual
Basic:
The spreadsheet is the interface itself, you don't need to
design and create an input form -- although you can
create one within Excel.
Many mathematical and non-mathematical functions are
accessible through the spreadsheet (see items 22 and 23
above) which are not available in Visual Basic.
Excel provides graphics which are relatively easy to
manipulate compared with graphics within Visual Basic.
Main disadvantage: cannot create stand-alone program
with Excel/VBA. The user need to have Excel in his or her
computer to be able to use your Excel/VBA programs.
4 of 5 8/14/2008 11:04 PM
http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction http://www.neng.usu.edu/cee/faculty/gurro/VBA&Excel.htm#Introduction
Evaluate(Application.WorksheetFunction.Substitute(expression, "x",
variable)) for evaluating expressions in worksheet cells.
4. Use of user forms for input/output in Excel/VBA.
5. Use of the VBA Object Browser to identify Excel objects, their properties
and methods.
6. Manipulation of Chart object properties.
5 of 5 8/14/2008 11:04 PM