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

Opening a Text File and Template from the Command Line

Word includes a command-line syntax that you can use to open files and do other operations. If you want to load a text file ...

Discover More

Automatically Selecting Words

When editing a document, Word normally selects entire words as you use the mouse to select text. This tip explains why this ...

Discover More

Quickly Moving Your Table

For those who love to use the mouse during editing, you can use the little critter to help move your tables to exactly where ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More

Copying Pictures with a Macro

Copying information using a macro is rather simple, although there are multiple ways you can do the copying. The most ...

Discover More

Installing the Date Picker

When creating your own user interfaces within Excel, the various developer controls can add a nice touch. One such control is ...

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 one more than 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.