+ Reply to Thread
Results 1 to 10 of 10

Editing Formulas with a Macro

  1. #1
    Registered User
    Join Date
    03-14-2008
    Posts
    45

    Editing Formulas with a Macro

    I'm trying to extend the range of a summation formula with a macro.

    I've attached a dummy worksheet. I've named each cell in row 8 as well as the summation ranges they are calculating.

    I am looking for the result of the macro to have each total's formula range to extend to to row 7 of its respective column.

    Any help with this is greatly appreciated!

    Andrew
    Attached Files Attached Files
    Last edited by adr150; 03-08-2010 at 01:52 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Editing Formulas with a Macro

    Simplest answer...

    1) Move your "totals" to row 1, above the data being evaluated.
    2) Change you FY10Range so that it refers to =Sheet1!$D$4:$D$1000
    ...etc.


    Next simplest

    1) Click on your data and press CTRL-L to activate the LIST function
    2) Now each time you add to the bottom of the list, your ranges should expand themselves
    3) Totals can stay below the data, outside the list range
    Last edited by JBeaucaire; 02-17-2010 at 07:25 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187

    Re: Editing Formulas with a Macro

    it is quite easy to do but why?

    If you are inserting rows the sum formula should extend anyway.


    click on the * Add Reputation if this was useful or entertaining.

  4. #4
    Registered User
    Join Date
    03-14-2008
    Posts
    45

    Re: Editing Formulas with a Macro

    while it has some awkward side effects (namely the blue border and seemingly slowing excel a bit),using the list works quite well. Thanks!

    tony h:
    The reason I'm asking is because I'm using a macro to insert rows and (before finding out that a list wold work) the sum formula wouldn't follow the new rows.

  5. #5
    Registered User
    Join Date
    03-14-2008
    Posts
    45

    Re: Editing Formulas with a Macro

    Quote Originally Posted by adr150 View Post
    while it has some awkward side effects (namely the blue border and seemingly slowing excel a bit),using the list works quite well. Thanks!
    Well, I've run into a problem with using a list. the data in the list is linked to another tab. The file in question will need to be replicated many times, but with slightly different information. Additionally, when I replicate the file, I sometimes need to copy the tab with the list and the tab that it is linked to within the same workbook.

    This is where the problem comes in: when you select both tabs, right click on the tab, and choose 'move or copy' and try to make a copy within the workbook, an error pops up saying "You cannot copy or move a group of sheets that contain a list."


    JBeaucaire suggested:

    1) Move your "totals" to row 1, above the data being evaluated.
    2) Change you FY10Range so that it refers to =Sheet1!$D$4:$D$1000
    ...etc.
    The problem with this solution is that below the first range is a second range that cannot be moved. So extending the formula down many rows isn't feasible.


    Any other suggestions? Can this be solved with a macro?

    Thanks again for the help!

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Editing Formulas with a Macro

    Let's see if we can set a defined dynamic range, still better than macros, IMO.
    Perhaps change the REFERS TO reference for the FY10Range to:

    =INDIRECT("Sheet1!$D$4:" & CELL("address",OFFSET(FY10Forecast,-1,0)))

    Or even better, since I can already see the next question coming regarding how to do this same thing in the next section below this one...

    =INDIRECT("Sheet1!" & INDEX(Sheet1!$D:$D, MATCH("2010 Forecast",Sheet1!$D:$D, 0) & ":" & CELL("address",OFFSET(FY10Forecast,-1,0))))

    For this to work, you cannot use the exact same column header titles in each table in the same column, at least one character needs to be different.

  7. #7
    Registered User
    Join Date
    03-14-2008
    Posts
    45

    Re: Editing Formulas with a Macro

    for some reason the indirect function isn't working on my computer. is there a setting that I need to adjust? just doing a simple =indirect(d4,false) returns #REF!

  8. #8
    Registered User
    Join Date
    03-14-2008
    Posts
    45

    Re: Editing Formulas with a Macro

    ahh got the basic indirect formula to work. didnt realize you need quotes around the cell reference.

    however, the formula
    =INDIRECT("Sheet1!$D$4:" & CELL("address",OFFSET(FY10Forecast,-1,0)))

    doesn't seem to be working as it returns #VALUE!
    Attached Files Attached Files

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Editing Formulas with a Macro

    Try this in D8:

    =SUM(INDIRECT("D4:" & CELL("address",OFFSET(FY10Forecast,-1,0))))

  10. #10
    Registered User
    Join Date
    03-14-2008
    Posts
    45

    Re: Editing Formulas with a Macro

    awesome, thanks!

    I actually just figured it out myself. I used a slightly different configuration, but the result seems to be the same

    here's what I used
    =SUM(INDIRECT("d4"):INDIRECT(CELL("address",OFFSET(FY10Forecast,-1,0))))

    anyway, this has been an interesting problem. i wasn't aware of the INDIRECT, CELL, or OFFSET functions before. a couple more tools for the toolbelt

    thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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