home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows / 1993_Aces_Research_Dr._Windows_UPC_765872322017_shareware.iso / misc / we0125.exe / WE0125T.TXT
Text File  |  1992-02-28  |  11KB  |  233 lines

  1. ======================================================================
  2.     Microsoft Product Support Services Application Note (Text File)
  3.                  WE0125: CELL REFERENCING FROM A MACRO
  4. ======================================================================
  5.                                                    Revision Date: 2/92
  6.                                                       No Disk Included
  7.  
  8. The following information applies to Microsoft Excel for Windows
  9. version 3.0.
  10.  
  11.  --------------------------------------------------------------------
  12. | INFORMATION PROVIDED IN THIS DOCUMENT AND ANY SOFTWARE THAT MAY    |
  13. | ACCOMPANY THIS DOCUMENT (collectively referred to as an            |
  14. | Application Note) IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY      |
  15. | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO    |
  16. | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A     |
  17. | PARTICULAR PURPOSE. The user assumes the entire risk as to the     |
  18. | accuracy and the use of this Application Note. This Application    |
  19. | Note may be copied and distributed subject to the following        |
  20. | conditions: 1) All text must be copied without modification and    |
  21. | all pages must be included; 2) If software is included, all files  |
  22. | on the disk(s) must be copied without modification [the MS-DOS(R)  |
  23. | utility DISKCOPY is appropriate for this purpose]; 3) All          |
  24. | components of this Application Note must be distributed together;  |
  25. | and 4) This Application Note may not be distributed for profit.    |
  26. |                                                                    |
  27. | Copyright 1992 Microsoft Corporation. All Rights Reserved.         |
  28. | Microsoft, MS-DOS, and the Microsoft logo are registered           |
  29. | trademarks and Windows is a trademark of Microsoft Corporation.    |
  30.  --------------------------------------------------------------------
  31.  
  32. Introduction
  33. ------------
  34.  
  35. Microsoft Excel has a powerful macro language that allows you to
  36. automate frequently performed tasks, customize functions, or create
  37. interactive applications. Cell referencing is an essential element in
  38. Microsoft Excel's macro language. To write a successful macro, you
  39. must understand the different ways to reference a cell. Macro commands
  40. take either relative or absolute references with respect to a cell, a
  41. range of cells, or defined ranges on either a worksheet or a macro
  42. sheet. In a macro, there are a variety of ways to refer to a cell,
  43. depending on whether the cell you are referring to is on a macro sheet
  44. or a worksheet and depending on which sheet is active when the macro
  45. command is executed.
  46.  
  47. Definitions
  48. -----------
  49.  
  50.    Term                  Definition
  51.    ----                  ----------
  52.  
  53.    Absolute reference    A reference that is fixed. It will not adjust
  54.                          itself if copied to another cell, if rows are
  55.                          inserted above it, or if columns are inserted
  56.                          to the left of it.
  57.    
  58.    Relative reference    A reference that will adjust itself if copied
  59.                          to another cell, if rows are inserted above
  60.                          it, or if columns are inserted to the left of
  61.                          it.
  62.    
  63.    Active sheet          The sheet from which the macro is run or the
  64.                          sheet that was last activated using the
  65.                          ACTIVATE function. (For more information on
  66.                          the ACTIVATE function, see page 2 of the
  67.                          "Microsoft Excel Function Reference.")
  68.    
  69.    Referencing style     Microsoft Excel uses two different
  70.                          referencing styles, A1 style referencing and
  71.                          R1C1 style referencing. You can use either
  72.                          style by choosing Workspace from the Options
  73.                          menu and selecting the R1C1 check box for
  74.                          R1C1 style referencing, or leaving the R1C1
  75.                          check box clear for A1 style referencing.
  76.  
  77. A1 Style Referencing
  78. --------------------
  79.  
  80.    Syntax                Description
  81.    ------                ----------- 
  82.  
  83.    $A$1                  This is an absolute reference to cell A1.
  84.    
  85.    A1                    This is a relative reference to cell A1.
  86.    
  87.    !$A$1                 This is an absolute reference to cell A1 on
  88.                          the ACTIVE sheet.
  89.    
  90.    !A1                   This is a relative reference to cell A1 on
  91.                          the active sheet.
  92.    
  93.    SHEET1.XLS!$A$1       This is an absolute reference to cell A1 on
  94.                          SHEET1.XLS.
  95.    
  96.    SHEET1.XLS!A1         This is a relative reference to cell A1 on
  97.                          SHEET1.XLS
  98.  
  99. R1C1 Style Referencing
  100. ----------------------
  101.  
  102.    Syntax                Description
  103.    ------                -----------
  104.  
  105.    R1C1                  This is an absolute reference to cell A1 (row
  106.                          1, column 1).
  107.    
  108.    R[1]C[1]              This is a relative reference to the cell that
  109.                          is one row below and one column to the right
  110.                          of the cell in which this reference is typed.
  111.                          Note: This reference is not relative to the
  112.                          currently active cell.
  113.    
  114.    !R1C1                 This is an absolute reference to cell A1 on
  115.                          the active sheet.
  116.    
  117.    !R[1]C[1]             This is a relative reference to the cell on
  118.                          the active sheet that is one row below and
  119.                          one column to the right of the cell in which
  120.                          this reference is typed. Note: This reference
  121.                          is not relative to the currently active cell.
  122.    
  123.    SHEET1.XLS!R1C1       This is an absolute reference to cell A1 on
  124.                          SHEET1.XLS.
  125.    
  126.    SHEET1.XLS!R[1]C[1]   This is a relative reference to the cell on
  127.                          SHEET1.XLS that is one row below and one cell
  128.                          to the right of the cell in which this
  129.                          reference is typed.
  130.  
  131. Tips for Using the SELECT Function
  132. ----------------------------------
  133.  
  134. Most macros need to select a particular cell or a range of cells in
  135. order to perform a certain task, whether it be to select a range of
  136. cells so they can be copied and pasted to a new range or to select one
  137. cell and determine what value that cell holds. The following are some
  138. tips for selecting cells from within a macro:
  139.  
  140.    -  =SELECT(A1) will always attempt to select cell A1 on the macro
  141.       sheet; however, it will always result in a macro error unless
  142.       the macro sheet is the active sheet.
  143.    
  144.    -  =SELECT(SHEET1.XLS!$A$1) or =SELECT(SHEET1.XLS!A1) will always
  145.       attempt to select cell A1 on SHEET1.XLS; however, it will always
  146.       result in a macro error unless SHEET1.XLS is the active sheet.
  147.    
  148.    -  =SELECT(R1C1) is not a valid macro statement (unless you are
  149.       using the R1C1 reference style by choosing Workspace from the
  150.       Options menu and selecting the R1C1 check box). The correct form
  151.       is =SELECT("R1C1"). This statement will select cell A1 of the
  152.       currently active sheet.
  153.    
  154.    -  Only five functions accept references in the form "R1C1" or
  155.       "R[1]C[1]". This quoted referencing style causes delayed parsing
  156.       of the cell reference. The quotation marks hold the cell
  157.       reference in its unparsed state until the line with the quoted
  158.       reference is evaluated by executing the macro. Attempting to use
  159.       one of these referencing forms in a function that is not
  160.       designed to accept it will result in a macro error or produce
  161.       incorrect results. For example, =IF("R1C1"=100) will always
  162.       return FALSE because "R1C1" is being treated as a text string,
  163.       and the IF statement will not evaluate it.
  164.    
  165.       Functions that can use absolute or relative references in the
  166.       "R1C1" or "R[1]C[1]" style are:
  167.  
  168.         Function    Description
  169.         --------    -----------
  170.  
  171.         SELECT      Can use both absolute and relative references in
  172.                     this style
  173.         TEXTREF     Can use absolute references only (R1C1)
  174.         INDIRECT    Can use absolute references only (R1C1)
  175.         ABSREF      Can use relative references only (R[1]C[1])
  176.         FORMULA     Can use both absolute and relative references in
  177.                     this style but ONLY in its first argument
  178.  
  179.    -  Activating worksheets and selecting cells can cause a macro to
  180.       run slowly. Quite often it is not actually necessary to select a
  181.       cell; instead you can use external references or the OFFSET
  182.       function to return information about a particular cell or range
  183.       of cells. For example,
  184.  
  185.         use:         =OFFSET(ACTIVE.CELL(),1,1)
  186.         instead of:  =SELECT("R[1]C[1]")
  187.  
  188.       to refer to the cell that is one row below and one column to the
  189.       right of the active cell.
  190.  
  191. Macro Debugging
  192. ---------------
  193.  
  194.    -  Step and Evaluate.
  195.       Microsoft Excel has a Step feature that allows you to step
  196.       through your macro line by line and evaluate your commands one
  197.       by one. This technique can be invaluable when trying to
  198.       determine why your macro is halting or not behaving as you
  199.       anticipated. To use the step feature, run your macro by choosing
  200.       Run from the Macro menu. After selecting your macro from the
  201.       list, choose the Step button. You can then either step or
  202.       evaluate the individual lines of your macro code. To evaluate a
  203.       line of code in stages, you can choose the Evaluate button
  204.       repeatedly.
  205.  
  206.    -  F9.
  207.       To evaluate a portion of a statement or an entire line of code
  208.       in your macro, use your mouse to select the area you want to
  209.       evaluate in the formula bar and press f9. This will immediately
  210.       evaluate the selected area. Be sure to press the esc key on the
  211.       keyboard after viewing the value, or the original formula will
  212.       be replaced with the value that is displayed in the formula bar.
  213.  
  214.    -  CTRL+ACCENT GRAVE.
  215.       To view the value of all statements on a macro sheet, press and
  216.       hold down the CTRL key and then press the ACCENT GRAVE key (`).
  217.       CTRL+ACCENT GRAVE is a toggle switch that puts the macro sheet
  218.       into View Values mode instead of View Formulas mode, which is
  219.       the default. Press CTRL+ACCENT GRAVE again to return the macro
  220.       sheet to View Formulas mode. An alternative way to toggle
  221.       between View Values and View Formulas is to choose Display from
  222.       the Options menu and select the Formulas check box or clear it
  223.       (when a macro sheet is active, the default is View Formulas).
  224.  
  225. More Information
  226. ----------------
  227.  
  228. For additional information on cell referencing, macro functions, and
  229. macro writing, see the "Complete Guide to Microsoft Excel Macros" by
  230. Charles Kyd and Chris Kinata, published by Microsoft Press.
  231.  
  232.  
  233.