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

Formatting Endnote Reference Marks

The reference marks used for endnotes are, by default, formatted "good enough" for most people. If you are one of those for ...

Discover More

Preventing Straggling Heads

Undoubtedly you will want to format your document so that headings stay with the paragraph that follows the heading. Here's ...

Discover More

Underlining Tabs In Numbered Lists

When Word creates an automatically numbered list, it removes some of your formatting flexibility. One thing you can't seem to ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Relative References when Recording Macros

When you record a macro, make sure that you know how Excel is recording your cell movements. This tip explains the problem ...

Discover More

Exiting a For ... Next Loop Early

If you use For ... Next loops in your macros, make sure you give a way to jump out of the loop early. That way you can limit ...

Discover More

Generating Unique, Sequential Names

Do you need to create a number of words or phrases where you only alter a few letters in each one? If the alterations follow ...

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 4 + 2?

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.