Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to record a macro using relative cell references in Excel


View products that this article applies to.

Summary

Unlike versions of Microsoft Excel earlier than Microsoft Excel 97, there is no menu command to record a macro that uses relative cell references in the versions of Excel listed at the beginning of this article. However, you can click Relative Reference on the Stop Recording toolbar.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS To record a macro that uses relative cell references, click Relative Reference on the Stop Recording toolbar. Clicking Relative Reference a second time turns off relative recording.

Note The ScreenTip for Relative Reference does not change to indicate whether relative recording is already available. The only indication that relative recording has started is the appearance of the Relative Reference button. When the button has a sunken or depressed three-dimensional appearance, relative recording has started.

If the Stop Recording toolbar does not appear when you begin recording a macro, you must display it. To display the Stop Recording toolbar, point to Toolbars on the View menu, and then click Stop Recording. When you do this, Microsoft Excel inserts an extra line of code in your macro. Note that when you run the recorded macro, the Stop Recording toolbar is displayed.

To avoid displaying the Stop Recording toolbar, follow these steps:
  1. On the Tools menu, point to Macro, and then click Macros.
  2. In Macro Name list, click the name of the macro that you recorded, and then click Edit.

    This step starts the Visual Basic Editor.
  3. In the Code window, type an apostrophe (') at the beginning of the following line of code:
    Application.CommandBars("Stop Recording").Visible = True
    					
  4. On the File menu, click Close and Return to Microsoft Excel.
NOTE: If you turn on relative recording, Microsoft Excel does not retain the setting for the next session of Microsoft Excel.

Absolute vs. Relative Recording

By default, recorded macros use absolute cell referencing, which means that exact cell locations are recorded into the macro. The macro works only with the exact cells addresses you use when you record the macro.

When you record a macro with relative cell referencing, the actions recorded in the macro are relative to the starting cell location.

The following examples record the same action with absolute, and then relative, cell referencing.

Recording with Absolute Cell Referencing

To record a macro with absolute cell referencing, follow these steps:
  1. Create a new workbook in Microsoft Excel, and then select cell C1 on the worksheet.
  2. On the Tools menu, point to Macro, and then click Record New Macro.
  3. In the Macro name box, type Absolute_Recording, and then click OK.
  4. On the Stop Recording toolbar, check the appearance of the Relative Reference button. If the Relative Reference button appears depressed, click it to turn off relative recording.
  5. Type test, and then click Enter (the green check mark on the Formula bar).
  6. On the Edit menu, click Copy, and then select cell C3. On the Edit menu, click Paste.
  7. On the Stop Recording toolbar, click Stop Recording.
  8. To view the contents of the macro you recorded, point to Macro on the Tools menu, and then click Macro. In Macro name list, click Absolute_Recording, and then click Edit.

    This step starts the Visual Basic Editor.

    The macro is similar to the following:
    Sub Absolute_Recording()
    '
    ' Absolute_Recording Macro
    ' Macro recorded 12/17/99 by <your username>
    '
    '
        ActiveCell.FormulaR1C1 = "test"
        Selection.Copy
        Range("C3").Select
        ActiveSheet.Paste
    End Sub
    						
    This macro enters the word "test" (without the quotation marks) in the active cell. Then, the macro copies and pastes the entry you typed in cell C3 regardless of the active cell location when you started the macro.
  9. When you have finished viewing the macro, click Close and Return to Microsoft Excel on the File menu.

Recording with Relative Cell Referencing

To record a macro with relative cell referencing, follow these steps:
  1. Create a new workbook in Microsoft Excel, and then select cell C1 on the worksheet.
  2. On the Tools menu, point to Macro, and then click Record New Macro.
  3. In the Macro name box type Relative_Recording, and then click OK.
  4. On the Stop Recording toolbar, check the appearance of the Relative Reference button. If the Relative Reference button does not appear to be depressed, click it to enable relative recording.
  5. Type test, and then click Enter (the green check mark on the Formula bar).
  6. On the Edit menu, click Copy, select cell C3, and then click Paste on the Edit menu.
  7. On the Stop Recording toolbar, click Stop Recording.
  8. Point to Macro on the Tools menu, and then click Macro. In Macro name list, click Relative_Recording, and then click Edit.

    This step starts the Visual Basic Editor.

    The macro is similar to the following:
    Sub Relative_Recording()
    '
    ' Relative_Recording Macro
    ' Macro recorded 12/17/99 by <your username>
    '
    '
        ActiveCell.FormulaR1C1 = "test"
        Selection.Copy
        ActiveCell.Offset(2,0).Range("A1").Select
        ActiveSheet.Paste
    End Sub
    						
    This macro enters the word "test" (without the quotation marks) in the active cell. Then, the macro copies and pastes the entry you typed to a cell location two rows down from the active cell.

↑ Back to the top


References

For more information about recording macros, click Microsoft Excel Help on the Help menu, type record a macro in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213740, kbui, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213740
Revision : 10
Created on : 1/24/2007
Published on : 1/24/2007
Exists online : False
Views : 357