Microsoft KB Archive/161677

= XL97: How to Record a Macro Using Relative Cell References =

PSS ID Number: 161677

Article Last Modified on 5/13/2002

-

The information in this article applies to:


 * Microsoft Excel 97 for Windows
 * Microsoft Visual Basic for Applications

-



This article was previously published under Q161677





SUMMARY
In Microsoft Excel 97, unlike in earlier versions of Microsoft Excel, there is no menu command to record a macro that uses relative cell references. However, you can click Relative Reference on the Stop Recording toolbar.



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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

http://www.microsoft.com/partner/referral/

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 is started is the appearance of the Relative Reference button. When the button has a sunken 3-dimensional appearance, relative recording is 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 this, follow these steps:  On the Tools menu, point to Macro, and then click Macros. In Macro Name list, click the name of the macro that you recorded and click Edit.

This step starts the Visual Basic Editor.  In the Code window, type an apostrophe (') at the beginning of the following line of code: Application.CommandBars("Stop Recording").Visible = True  On the File menu, click "Close and Return to Microsoft Excel."

NOTE: If you enable 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:  Create a new workbook in Microsoft Excel.</li> Select cell C1 on the worksheet.</li> On the Tools menu, point to Macro, and then click Record New Macro.</li> In the Macro Name box type Absolute_Recording and then click OK.</li> On the Stop Recording toolbar, check the appearance of the Relative Reference button. If the Relative Reference button has a sunken 3-dimensional appearance, click it to disable relative recording.</li> Type test and click Enter (green check mark).</li> On the Edit menu, click Copy.</li> Select cell C3.</li> On the Edit menu, click Paste.</li> On the Stop Recording toolbar, click Stop Recording.</li>  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 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/27/96 by       ' '          ActiveCell.FormulaR1C1 = "test" Selection.Copy Range("C3").Select ActiveSheet.Paste End Sub This macro enters the word test, 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. </li> When you are finished viewing the macro, click "Close and Return to Microsoft Excel" on the File menu.</li></ol>

Recording with Relative Cell Referencing
To record a macro with relative cell referencing, follow these steps:  Create a new workbook in Microsoft Excel.</li> Select cell C1 on the worksheet.</li> On the Tools menu, point to Macro, and then click Record New Macro.</li> In the Macro Name box type Relative_Recording and click OK.</li> <li>On the Stop Recording toolbar, check the appearance of the Relative Reference button. If the Relative Reference button does not have a sunken 3-dimensional appearance, click it to enable relative recording.</li> <li>Type test and click Enter (green check mark).</li> <li>On the Edit menu, click Copy.</li> <li>Select cell C3.</li> <li>On the Edit menu, click Paste.</li> <li>On the Stop Recording toolbar, click Stop Recording.</li> <li> To view the contents of the macro you recorded, point to Macro on the Tools menu and click Macro. In Macro Name list, click "Relative Recording" and 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/27/96 by        ' '           ActiveCell.FormulaR1C1 = "test" Selection.Copy ActiveCell.Offset(2,0).Range("A1").Select ActiveSheet.Paste End Sub This macro enters the word test 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. </li> <li>When you are finished viewing the macro, click "Close and Return to Microsoft Excel" on the File menu.</li></ol>

<div class="references_section">