Using R1C1 Formula References in a Macro

Written by Allen Wyatt (last updated September 16, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Gerry finds it easiest to put together formulas that use R1C1 cell references. She knows how to do this when manually adding formulas to a worksheet, but wonders how she can use R1C1 references in formulas that she puts together and stuffs into cells using a macro. She wonders if there is anything she needs to watch out for when doing this.

When you are normally stuffing a formula into a cell, you would use this type of syntax in your macro:

Cells(3,1).Formula = "=A1 + A2"
Range("A3").Formula = "=A1 + A2"

Either of these will work fine; they both stuff a simple formula into cell A3. If, however, you want to use R1C1 references in the formula you place into cell A3, you only need to change the Formula property to the FormulaR1C1 property:

Cells(3,1).FormulaR1C1 = "=R1C1 + R2C1"
Range("A3").FormulaR1C1 = "=R1C1 + R2C1"

It is interesting to note that if you place the above R1C1 formulas into a cell and the worksheet doesn't have R1C1 display turned on, then Excel converts the formula to reflect the display that is active. In other words, it automatically changes "=R1C1 + R2C1" to "=$A$1 + $A$2". The opposite is also true—place the formula "=A1 + A2" into a cell, and it displays as " =R[-2]C + R[-1]C" if you have R1C1 display turned on.

You should also note that since both Formula and FormulaR1C1 are properties, you can read them and see the formula in the cell in the desired format. For instance, let's say cell A3 contains the formula "=A1 + A2". If you then run the following macro, you'll see the formula displayed in the desired formats:

Sub TestFormula()
    Dim sMsg As String

    sMsg = "Regular format: " & Cells(3,1).Formula & vbCrLf
    sMsg = sMsg & "R1C1 format: " & Range("A3").FormulaR1C1
    MsgBox sMsg
End Sub

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (5294) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Printing Graphic Thumbnails

If you are doing work with a lot of graphics, it may be helpful to create a summary page that contains thumbnail ...

Discover More

AutoFitting Tables

Need to adjust the width of a bunch of table columns according to what is in the columns? Word provides a tool to do ...

Discover More

TOC Heading Numbers Always Show in Bold

Linda's got a document that includes a table of contents that is based on headings in the document. When the headings ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Making Worksheet Copies for Daily Shifts

Creating lot of copies of a worksheet is a snap within a macro. This tip introduces a macro that will make three copies ...

Discover More

Executing a Macro Every 15 Minutes

Need to run a macro at a given interval? It's easy to do when you learn how to use the .OnTime method, described in this tip.

Discover More

Calculating an ISBN Check Digit

ISBNs are used to uniquely identify books. You may need to know if an ISBN is valid or not, which involves calculating ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 0?

2023-09-16 10:37:37

J. Woolley

The Tip mentions "...if you have R1C1 display turned on." My Excel Toolbox includes the ToggleRefereceStyle macro (Ctrl+T A 1) to switch between A1 and R1C1. The macro supports Undo (Ctrl+Z). Here is an abbreviated version:

Sub ToggleReferenceStyle()
    Const myName As String = "ToggleReferenceStyle"
    With Application
        .ReferenceStyle = IIf(.ReferenceStyle = xlA1, xlR1C1, xlA1)
        .OnUndo myName, (ThisWorkbook.Name + "!" + myName)
    End With
End Sub

See https://sites.google.com/view/MyExcelToolbox


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.