Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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.
Written by Allen Wyatt (last updated October 11, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12919) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Changing Macro Cell References Based on Edits.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
When you change from one worksheet to another, you may want to have Excel automatically run a macro for the worksheet you ...
Discover MoreMacros are often used to process information in a worksheet. You may need your macro to change the values stored in ...
Discover MoreWant a quick way to jump to the end of your data entry area in a worksheet? The macro in this tip makes quick work of the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments