+ Reply to Thread
Results 1 to 5 of 5

Keeping macro cell references when adding/removing rows/columns

  1. #1
    Registered User
    Join Date
    04-06-2009
    Location
    Santa Monica, CA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Keeping macro cell references when adding/removing rows/columns

    Is there a way to use cell references in macros so that the cell references are automatically adjusted if row/columns are added/removed (the way that formulas in a worksheet automatically adjust)? As far as I can tell, I would either have to name every range that is used in the macro, or go into the VBA and manually adjust all affected cell references. Thanks.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Keeping macro cell references when adding/removing rows/columns

    Hello dlukas,

    Welcome to the Forum!

    Your question is to general to answer. You should either post an example of your code or what you want the code to do (before and after). Range objects are the backbone of Excel and there are many ways to use them. Without knowing what you want to accomplish, posting a solution becomes a guessing a game. The more detail you can provide, the faster you will receive an answer.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-06-2009
    Location
    Santa Monica, CA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Keeping macro cell references when adding/removing rows/columns

    I haven't written the code yet, but essentially this is what it will do (the inputs for the macro will be hardcoded in a range of cells in a financial model):

    1. copy the first value in the range
    2. paste the value as one of the model's inputs
    3. copy a specific output from the model
    4. paste it into a separate output table created by the macro
    5. loop back and copy the second hardcoded item, repeat steps 2-5

    Ultimately the table that the macro creates will be a sensitivity analysis, showing how the selected output changes as the specified input changes. Once I get it working I will expand the functionality to perform multiple sensitivity analyses between any variables chosen... however the problem is that the model itself may have to be changed concurrently. If that happens, it will throw off all the cell refereces for the model's inputs and outputs. If there's a way to avoid this so that the cell references in the macro "slide" along with row/column changes in the model it would save a LOT of time.

    Hopefully that gives enough background... thanks all!

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Keeping macro cell references when adding/removing rows/columns

    Quote Originally Posted by dlukas View Post
    Is there a way to use cell references in macros so that the cell references are automatically adjusted if row/columns are added/removed (the way that formulas in a worksheet automatically adjust)? As far as I can tell, I would either have to name every range that is used in the macro, or go into the VBA and manually adjust all affected cell references. Thanks.
    I believe you are right. You have to name the ranges or manually adjust all references.

    If there is another solution, please bump this thread to the top so I can see the solution.

  5. #5
    Registered User
    Join Date
    04-06-2009
    Location
    Santa Monica, CA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Keeping macro cell references when adding/removing rows/columns

    That's what I'm beginning to suspect. For this very specific situation only, I think I can eliminate some of the work that the macro would need to do by using a combination of data tables that reference INDIRECT formulas that reference INDEX formulas... the fundamental problem of non-sliding cell references remains but at least now I have reduced the number of ranges I have to name in order to address the problem.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1