Sie sind auf Seite 1von 4

VBA: Visual Basic for Applications - Introduction

WhatisVBA?

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
browser). In MS Office applications VBA is used mainly to develop procedures, known as Macros, that
can be run within the application itself.

By using VBA within a given application you can use the application's OWl1interface to run a program (a
macro) rather than creating your OWl1interface 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 numericalprocedure. 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. AlI the
VB commands, such as Open, Close, Input, Print, If blocks, Do While, Do Until, For ... Next, etc., are also
available with.inVBA ~-

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 min.imum
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
check out. 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 programo Those
input cells in the worksheet can be referenced within a VBA program (macro) by using the VBA functions
Range and Cells. As an example of a simple VBA application within Excel, open an Excel worksheet and
fill out the cells as ShOWl1in the figure below. The contents of cells AI and A3 are simply strings that
cover additional cells to the right. These strings are entered by clicking on the corresponding cell and
typing the string directly. The contents of cells A5 and B5 are the strings "x" and "y", r~spectively. Again,
entered by typing directly on those cells.

Creating a Table o{values


The problem we are trying to solve is to find the solution to the equation f(x) = O (see figure below) in the
interval (2,5). Therefore, it will be convenient to have a graph of the function available to estimate the
location of the root or roots. This, as you know, is easily achieved in Excel by typing values of x in the
cells inunediately below cell A5, and, then entering the formula corresponding to f(x) in the cells
inunediately to the right, i.e., below cell B5. To fill out the first entry in the table, type the value 2 in cell
A6, and then, in cell B6, type the formula:

= Exp(A6)*Sin(2.*A6)-3.5

VBAlntro
~ "

Next, in cell A 7 enter the formula

= A6 + 0.1

Then, click on cell Bô to select the cell. place the cursor 011the lower right comer of that cell, then, holding
the left bUtt011011the 1110use, drag the mouse until the cell B7 is selected. Release the mouse bUtt011. What
you have just done is to copy the formula in cell Bô 011tOcell B7. Because the formula i11Bô refers to the
relative position Aô, when copying the same formula to cell B7, Excel modifies to formula to refer to the
corresponding adjacent cell in the x column, namely A 7, thus completing the second entry into the table,

To complete the table up to x = 5, select cells A 7 and B7 simultaneously by holdíng down the left 1110use
button, placing the cursoron the lower right comer of cell B7, and draggíng the mouse down several rows.
Stop dragging the mouse when the value 5.0 shows up somewhere in the A column. For this case, that
happens when we reach TO\\' number 36. As ShO\\11 in the fignre below, we now have a complete table of y
= fix), in the domain (2,5) .

.. we creste a tabls using Exc~l: .. ,p:arameter~ for lhe Newton-Raphson solution:

:y = f()() .epsilon: 0.0001 :


2 -8.84206; 'Nm<lx: 1000"
2.1 -10.3674: )(0: 3
2,2 -11.8382
2.3 -13.16n: 'Solution: 3.21229·'
2.4" -142309' Errar: 2.31E-09
2.5' -14.9321',
2,6. -15,1446:
2.7 -14.7485:
2.8 ·13.631 !
2,9" -fÜ937~! ,

j]~f~ 1
3,~ ~~:~~~~~ ; .
'!:~R@~\jj:2 "~Ó39iJ15 ':":' ..
,:;;;rg;ii 3.35.196708: .

~:f~t4!i\f""'ii\~~~'i~'if!

Figure L Spreadsheet for ca leu lat ing roots of a funct i011us ing the Newton-Raphson m ethod.
. .

v'BATntro
Solution of the equation: f(x) = exp(x)*sin(2*x)-3.25
1
= ° using the Newton-Raphson
? " ~
method in the range (2,5).

First we create a table using Excel: Parameters for the Newton-Raphson solution:

x y =f(x) epsilon: 0,0001


2 -8,84206 Nmax: 1000
2,1 -10,3674 xO: 5
2,2 -11,8382
2,3 -13,1613 Solution: 4,696412
2,4 -14,2309 Error: -5,2E-09
2,5 -14,9321
2,6 -15,1446
2,7 -14,7485
2,8 -13,631
2,9 -11,6937
3 -8,86221
3,1 -5,09441
3,2 -0,39075
3,3 5,196708
3,4 11,55566
3,5 18,50641
3,6 25,79684
3,7 33,10032
3,8 40,01716
3,9 46,08049
4 50,76713
4,1 53,51395
4,2 53,74007
4,3 50,87491
4,4 44,39201
4,5 33,84772
4,6 18,92405
4,7 -0,52601
4,8 -24,4325
4,9 -52,4644
5 -83,9899
Sheetl - l

I'I: iVõl.te SuJ:. cmdNe1):n::c.n_Cli<::lt( )


D i:rr. x &'5 DouJ:.l e
D ir,., eI''5 i1 on &'5 Do,J.J:.1e
D im N'm.v. z,.'5 lnt e ge I:
Dim i &'5 lnt:egeI:

=
eI''5 i1 OTL Rõl.nge ( "F5")
N'm.•.•, =
Rõl.TLge( "F6 " )
x = Rõl.nge ( "F?")

i = O

Do 1jJhil e z,.J:.'5
(f (x)) )- eI''5 il OTL.~..nd i -c N'm=
•.• = x - f (•.•) / fI' (.'. )
i =
i + l
( "F lO " ) • I]õl.1ue = f'( x )
Rõl.TLge
Loe p

lf Z,..J:.'5(f(•.•)) -c eI''5i1.~n Tl.••


::n
Rõl.TLge ( "F9 " ) • I]õl.1'~e = x
E1'5e
H:'5gBo•.• ("N.:;. conveI:gence õl.ft:eI: " & StI:( i) & " iteI:õl.t:ion'5 - tI:}' ne1)' iTdtiõl.1 võl.1ue. ")
End If

I'I: iVõl.t:e F1~ct: ion f ( x &'5 Do,J.J:.1e) &'5 DouJ:.1 e


f = E•.•I' (x) -r. S in (2# -r. •.•) - :3. 5
End F'lJ.TLct: i OTL
I'I: iVõl.t:e ~ct ion fI' ( •.• l-.'5 Dc.uJ:.1e) &'5 Do,J.J:.1e
fI' = ExI' (x) -r. (S iTL(2# -r. x ) + 2# -r. C0'5 (2# -r. x ) )
End F1rrL<::ti on

Das könnte Ihnen auch gefallen