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

28                                         EXCEL: NUMERICAL METHODS



               The Variant Data Type
                   The Variant data type is the default data type in VBA.  Like Excel itself, the
               Variant  data type  handles  and  interconverts  between  many  different  kinds  of
               data:  integer,  floating  point,  string,  etc.  The  Variant data type  automatically
               chooses the most compact representation.  But if your procedure deals with only
               one  kind  of  data,  it  will  be  more  efficient  and  usually  faster  to  declare  the
               variables as, for example, Integer.



               Subroutines

                   By "subroutine" we mean a Sub procedure that is kalled" by another VBA
               program.  In writing a VBA procedure,  it may be necessary to repeat the same
               instructions several times within the procedure.  Instead of repeating the same
               lines  of  code  over  and  over  in  your  procedure,  you  can  place  this  code  in  a
               separate  Sub program;  this  subroutine or subprogram  is then  executed  by  the
               main program each time it is required.
                   There are several ways to execute a subroutine within a main program.  The
               two most common are by using the Call command, or by using the name of the
               subroutine.  These are illustrated in Figure 2-1 1.  Mainprogram calls subroutines
               Taskl  and  Task2,  each  of which  requires  arguments that  are passed  from the
               main program to the subroutine and/or are returned  from the subroutine to the
               main program.

                   Sub Mainprogram()
                      etc.
                   Call Taskl (argument1 ,argument2)
                      e tc
                   Task2 argurnent3,argurnent4
                      efc
                   End Sub

                   Sub Taskl (ArgNamel ,ArgName2)
                       efc
                   End Sub

                   Sub Task2(ArgName3,ArgName4)
                      etc
                   End Sub
                  Figure 2-11.  A main program illustrating the different syntax of subroutine calls.
                   The two methods use different syntax if the subroutine requires arguments.
               If the Call command is used, the arguments must be enclosed in parentheses.  If
               only the subroutine name is used, the parentheses must be omitted.  Note that the
   46   47   48   49   50   51   52   53   54   55   56