Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Changing Macro Cell References Based on Edits.

Changing Macro Cell References Based on Edits

by Allen Wyatt
(last updated September 5, 2017)

2

David wonders if there is any way for cell references in a macro to change when adding or deleting rows, similar to the way a formula responds to such changes?

When you reference a cell in a macro, such as using Range("B6"), then VBA treats that reference as absolute, meaning that it doesn't change. Even if you add or delete cells that affect where the info that was in B6 is now located, the macro reference will remain the same.

The way around this is to not use direct references to cells in your macros. Instead, rely on named ranges. In Excel, define a name for cell B6 (such as "MyData"), and then use that name in the reference, as in Range("MyData"). This approach works because VBA looks up the name in order to determine which cell is being referenced, and Excel makes sure the named range references remain up-to-date as you add or delete cells.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12919) applies to Microsoft Excel 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Excel here: Changing Macro Cell References Based on Edits.

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

Scrolling Up and Down

Need an easy way to move through a spreadsheet using a mouse? Here are a couple of ideas.

Discover More

Getting Rid of Non-Printing Characters Intelligently

Is your worksheet, imported from an external source, plagued by non-printing characters that show up like small boxes ...

Discover More

Finding the Nth Root of a Number

Finding a square root is easy because Excel provides a worksheet function for that purpose. Finding a different root may ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Replacing Some Formulas with the Formula Results

Macros are often used to process the data stored in a worksheet. Some of these processing needs can be pretty specific to ...

Discover More

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come ...

Discover More

Running a Macro when a Workbook is Closed

One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...

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}] 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 5 + 9?

2015-01-24 07:09:59

abhijit

[1] I want customized text formatting i.e. Text_Colour, Bold, Italic, Underline with any preferable font individually or jointly in the formula givne below (using CONCATENATE and/or without using CONCATENATE) as result.

[2] I want to use the Indian Rupee sign replacing ` in in the same formula i.e. CONCATENATE.

FORMULA:-
=CONCATENATE(" ","Now, total monthly family income is ",(AC31),TEXT(AD31,"00,000.00")," falls below / above 90% of Gross Monthly Salary ",(AC31),TEXT(L12,"00,000.00")," but falls below / above ",(AC31),TEXT(AD10,"00,000.00")," minimum Salary of L.D.C. / Group-'D'. So, may be eligible / not eligible for consideration.")


2014-01-27 09:49:35

Bryan

There are many ways around this problem, depending on what, exactly, you are trying to accomplish. For example, instead of deleting cells/rows one at a time, I've had success adding each range to be deleted to a master deletion range (using Union), then deleting them all at the end of the macro.

The most important part of Allens tip is that you should almost never hard-code cell references.


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.