Page 28 - Excel for Scientists and Engineers: Numerical Methods
P. 28

CHAPTER 1  INTRODUCING VISUAL BASIC FOR APPLICATIONS                   5


                procedures  with arguments.  Empty parentheses are still required  even if a Sub
                procedure uses no arguments.


                   Sub ProcedureName(Argument1, ...)
                       VBA statements
                   End Sub
                                  Figure 1- 4.  Structure of a Sub procedure.


                The Structure of a Function Procedure
                   The  structure  of  a  Function  procedure  is  shown  in  Figure  1-5.  The
                procedure begins with the keyword Function  and ends with End  Function.  It
                has a FunctionName, a unique identifier that you assign to it.  The name should be
                long enough to indicate the purpose of the function, but not too long, since you
                will  probably  be typing  it  in  your worksheet formulas.  A  Function procedure
                usually takes one or more arguments; the names of the arguments should also be
                descriptive.  Empty parentheses are required even if a Function procedure takes
                no arguments.

                   Function FunctionName(Argument1, ...)
                       VBA statements
                       FunctionName = result
                   End Function

                                Figure 1-5.  Structure of a user-defined function.
                   The function's  return statement  directs the procedure to return the result to
                the  caller  (usually  the  cell  in  which  the  function  was  entered).  The  return
                statement consists of an assignment statement in which the name of the function
                is equated to a value, for example,
                       FunctionName = result

                Using the Recorder to Create a Sub Procedure

                   Excel  provides  the  Recorder,  a  useful  tool  for creating command  macros.
                When you choose Macro from the Tools menu and Record New Macro.. . from
                the submenu, all subsequent menu  and keyboard actions will be recorded  until
                you  press  the  Stop Macro  button  or choose  Stop Recording from the Macro
                submenu.  The Recorder  is convenient  for creating simple macros that  involve
                only the use of menu or keyboard commands, but you can't use it to incorporate
                logic, branching or looping.
                   The  Recorder  creates  Visual  Basic  commands.   You  don't  have  to  know
                anything about Visual Basic to record a command macro in Visual Basic.  This
                provides a good way to gain some familiarity with Visual Basic.
   23   24   25   26   27   28   29   30   31   32   33