+ Reply to Thread
Results 1 to 4 of 4

VBA to automatically extend a SUM formula

  1. #1
    Registered User
    Join Date
    01-08-2014
    Location
    Bath, UK
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    86

    VBA to automatically extend a SUM formula

    Hi

    I have a relatively basic table and am trying to automatically populate/refresh the total in a column after someone inserts a new row at the end of the column, but the code I have found doesn't seem to work (in spite of comments in the earlier threads suggesting it does work - am I misinterpreting something, or did they tweak something..?)

    I have data in column Q, ranging from Q5 to Q44 (rows 1-4 are heading rows).
    I have a simple SUM formula in Q45 - i.e. SUM(Q5:Q44)

    Nice and easy so far...

    The user can insert a row beneath the last line of data, the new row would be row 45, with the SUM moving down to row 46. As expected, the SUM refers to the original range (Q5:Q44) but needs updating to (Q5:Q45). Unfortunately, the user cannot be trusted to amend the SUM range or to copy it across to the other columns. I am trying to create a macro which does this automatically.

    The code I have found is:
    Please Login or Register  to view this content.
    (the variables are populated elsewhere in the macro, rather than being entered manually within the macro - the issue is not with the variables, but with the SUM formula)

    However, when I run this, I get a #NAME? error and the formula in the cell is =SUM('Q5':'Q45') (with the single quotes) - i.e. the range has been updated correctly, but the single quotes are causing a problem.
    I can create a simple macro to remove the quote marks (using the edit/replace (Ctrl+H) function - I'm a VBA novice, so don't know a better way), but I assume there would be a minor tweak to get the macro to populate the formula correctly, i.e. =SUM(Q5:Q45)


    P.S. I have considered, but discarded, using a named range to automatically extend the range, but am not sure that would work because the SUM row is directly beneath the data and it would be tricky (not impossible) to identify a blank cell, for example, to indicate the end of the dynamic named range


    Can someone help, please?

    Thanks,
    Graham

  2. #2
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: VBA to automatically extend a SUM formula

    If you use the FormulaR1C1, you need to provide row number and column number.

    example:
    Please Login or Register  to view this content.
    In your case you should use the Formula statement

    Please Login or Register  to view this content.
    this should give you the correct formula.
    Even a little help can be a big help !!
    1. A sample workbook says more then words. Add problem description and solution so we can understand the problem.
    2. Your appreciation is accepted by clicking the star "Add Reputation" at the lower left of the post.
    3. If your problem is solved, mark it as [SOLVED]: See "FAQ : " https://www.excelforum.com/faq.php
    4. Use [CODE] [/CODE] tags to illustrate your code: see here

  3. #3
    Registered User
    Join Date
    01-08-2014
    Location
    Bath, UK
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    86

    Re: VBA to automatically extend a SUM formula

    Brilliant MarMo - that works perfectly!!! Thank you very much and thank you for the prompt reply!!

    (and thanks for the brief explanation too!)

  4. #4
    Forum Contributor
    Join Date
    09-21-2009
    Location
    Belgium - Mechelen
    MS-Off Ver
    Office 365 - version 2310
    Posts
    278

    Re: VBA to automatically extend a SUM formula

    You're very welcome !

    I've attached a file with some code that searches for the last cell in column Q, and uses this in the code to give it the correct range.

    Please Login or Register  to view this content.
    Hope this helps.
    Attached Files Attached Files
    Last edited by MarMo; 07-27-2018 at 05:36 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. VBA Code to Automatically Extend\Add Formulas Down As New Data is Added
    By Kimston in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-15-2018, 06:12 PM
  2. Extend range of table automatically to fit data
    By b624333 in forum Excel General
    Replies: 1
    Last Post: 07-14-2014, 01:00 PM
  3. Replies: 2
    Last Post: 05-29-2014, 05:03 AM
  4. Chart Automatically update and extend
    By surfengine in forum Excel General
    Replies: 1
    Last Post: 11-17-2006, 01:19 PM
  5. How to Extend Range Automatically
    By myeaddress2003 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-14-2006, 06:45 AM
  6. Replies: 1
    Last Post: 10-13-2005, 02:05 PM
  7. [SOLVED] Excel should not automatically extend series or fill values
    By whisperlm in forum Excel General
    Replies: 0
    Last Post: 07-13-2005, 03:05 PM

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