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.

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

Saving and Closing All Open Documents

Want to close or save all your documents at the same time? This trick does it for you.

Discover More

Conditionally Making a Sound

Need to have a sound played if a certain condition is met? It is rather easy to do if you use a user-defined function to ...

Discover More

Finding Duplicate Documents

Over time, it is very possible to end up with multiple copies of the same document on your system. If you have (or ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

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

Cropping Graphics in a Macro

Excel allows you to easily paste graphics into a worksheet. Once added, you may want to quickly process the graphics by ...

Discover More

Controlling Window Size when Opening Additional Workbooks

When you open multiple workbooks, the way in which Excel sizes them is not the best for your needs. This tip looks at a ...

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 nine minus 4?

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.