Page 142 - Excel Progamming Weekend Crash Course
P. 142

h540629 ch09.qxd  9/2/03  9:34 AM  Page 117




                  Session 9 — Working with Text                                          117

                  Listing 9-1 also includes a procedure named TestIsNumber that lets you test the
               IsNumber function. The code in this procedure uses the InputBox function to prompt the
               user to enter an integer number. The IsNumber function then tests whether the text
               entered by the user is, in fact, a number. The loop repeats until a valid number is entered.
                          It would be a good programming exercise for you to modify the IsNumber
                          function so it also accepts numbers with a decimal point or a leading minus
                  Note    sign.
                  To run this demonstration, follow these steps:

                 1. Open the VBA Editor.
                 2. Open the code-editing window for one of the modules in the Project Explorer win-
                    dow. Because this program does not interact with a worksheet, it does not matter
                    which module you use.
                 3. Insert the code from Listing 9-1 into the module.
                 4. Place the editing cursor anywhere inside the TestIsNumber procedure.
                 5. Press F5 to run the program.

                          In addition to pressing F5, you can run a VBA procedure by clicking the Run
                          Sub button on the toolbar, or by selecting Run Sub from the Run menu.
                   Tip

               The Chr Function

               This function returns the character that corresponds to a specific numeric code. The syntax is:
                  Chr(code)

                  The code argument is the numeric code of interest. The function returns a string con-
               taining the corresponding character. If code is out of range, an error occurs.


               Extracting or Modifying Parts of Strings

               The VBA functions that are covered in this section are used when you need to extract or
               modify part of a string.


               The Left and Right Functions
               These functions extract a specified number of characters from either the left or the right
               end of a string. The syntax is:
                  Left(string, length)
                  Right(string, length)
   137   138   139   140   141   142   143   144   145   146   147