Page 36 - Excel Progamming Weekend Crash Course
P. 36

d540629 ch01.qxd  9/2/03  9:27 AM  Page 11




                  Session 1 — Microsoft Excel Programming — Why and How                   11

               write a great program, but if it is not what the customer needs you have wasted your time!
               Don’t assume that you know what is wanted — make sure, in detail.
                  After you know your goal, start planning the details of the program. For simple programs
               this step may be trivial because there is only one way to accomplish the desired ends. With
               more complex programs, you have choices to make. How many worksheets and workbooks
               will be needed? Will any user forms be required? How will the functionality be divided up?
               Should you define any classes for the project? Of course, plans that you make at this stage
               are not set in stone — you can always modify them later as circumstances dictate. Even so,
               having some plan at the outset, even if it is an incomplete plan, is a real help.


               Your First Excel Program

               To give you a taste for Excel programming, this section presents a short program that illus-
               trates some of the basic principles. You are not expected to understand all the details of the
               program at this point, but working through the required procedures will help in under-
               standing the material in the following sessions.
                  This program does not do anything complicated. The desired goal is to create a program
               that does the following:
                 1. Starts with a blank worksheet.
                 2. Enables the user to enter a series of numbers in specified cells.
                 3. Calculates the sum of the numbers.
                 4. Saves the workbook to disk.
                  The following sections guide you through the process of creating and running this program.


               Creating and Naming the Program
               These first steps create the basic program structure and assign a name to it.

                 1. Start Excel. As usual, it starts with three blank workbooks displayed.
                 2. Press Alt+F11 to open the VBA Editor.
                 3. At the top left of the VBA Editor, there is a window labeled “Project - VBAProject”
                    that lists the currently open worksheets: Sheet1, Sheet2, and Sheet3, as shown in
                    Figure 1-2. Double-click the Sheet1 entry, and a code-editing window opens.

                 4. Select Procedure from the Insert menu. The Add Procedure dialog box displays.
                    Enter a name for your program, such as MyFirstProgram. You can use letters and
                    numbers, but no spaces (some programmers use an underscore in place of spaces
                    to separate words).
                 5. Click OK. The VBA Editor inserts the first and last lines of the procedure (program)
                    in the code-editing window (see Figure 1-3).
                  At this point, you have created the empty “skeleton” of your program. Next, you add the
               code that provides its functionality.
   31   32   33   34   35   36   37   38   39   40   41