+ Reply to Thread
Results 1 to 7 of 7

Macro help to embed a SUM Function for tabulating the sum of a column or row

Hybrid View

  1. #1
    Registered User
    Join Date
    06-09-2013
    Location
    Oklahoma
    MS-Off Ver
    Excel 2010
    Posts
    24

    Macro help to embed a SUM Function for tabulating the sum of a column or row

    I am writing a macro that calls up another spreadsheet file consisting of column data. The macro will perform some
    sort routines and simple additions and finally save the edited file as a new excel file. The new data file will need to
    have a few formulas embedded. The area I am seeking help with is how to embed a few simple excel formulas in
    certain cells. The task would be easy if the data file always had the same number of rows. However the number of
    rows will vary and hence the location of the embedded excel formula is dependent upon the location of the "last" row
    of data.

    Basically looking for help to accomplish three aspects:

    1) embed the excel function =SUM(A12:Axx) ... where xx is the 2nd to last row of data in column A
    The embedded function will be located in Cell Azz ... where zz is the last row of column A

    2) Cell(J1) needs the formula =Azz .. where this formula is embedded to display the value of (Azz)

    3) Cell(K1) needs the formula =SUM(Axx:Sxx) .. where xx is the last row # and is used to display the Row Sum of Axx thru Sxx

    The reason the embedded formulas are important for my project is because the newly saved spreadsheet will be modified manually by changing some of the data values. When the data values are altered by the user, the embedded formulas will calculate the
    updated totals.

    Thank you in advance for considering my dilemma as I am a novice desiring to learn more about coding of macros.
    Any help or assistance is greatly appreciated.
    Cheers
    Last edited by D18GE; 06-12-2013 at 12:09 AM.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro help to embed a SUM Function for tabulating the sum of a column or row

    Maybe?

    Sub D18GE()
    Range("A" & Rows.Count).End(3)(2).Formula = "=SUM(A12:A" & Range("A" & Rows.Count).End(3)(1).Row & ")"
    Range("J1").Formula = "=sum(A" & Range("A" & Rows.Count).End(3)(1).Row & ")"
    Range("K1").Formula = "=SUM(A" & Range("A" & Rows.Count).End(3)(1).Row - 1 & ":S" & Range("A" & Rows.Count).End(3)(1).Row - 1 & ")"
    
    End Sub

  3. #3
    Registered User
    Join Date
    06-09-2013
    Location
    Oklahoma
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Macro help to embed a SUM Function for tabulating the sum of a column or row

    John, thank you for your prompt reply and coding. I tried the code and it's almost there. The one issue I notice
    is the formula for summing the column does not appear on the last row. It appears on the last row + 1
    The data file this macro calls up already has a hard coded value in this location. I understand from another
    forum member it may be worthwhile to clear the bottom row before executing. Not sure if that helps, but I don't
    know how to clear the last row myself. That is the only issue I have noticed. Many thanks.

  4. #4
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro help to embed a SUM Function for tabulating the sum of a column or row

    You're welcome. Try this.

    Sub D18GE()
    Range("A" & Rows.Count).End(3)(2).Formula = "=SUM(A12:A" & Range("A" & Rows.Count).End(3)(1).Row -1 & ")"
    Range("J1").Formula = "=sum(A" & Range("A" & Rows.Count).End(3)(1).Row & ")"
    Range("K1").Formula = "=SUM(A" & Range("A" & Rows.Count).End(3)(1).Row - 1 & ":S" & Range("A" & Rows.Count).End(3)(1).Row - 1 & ")"
    
    End Sub

  5. #5
    Registered User
    Join Date
    06-09-2013
    Location
    Oklahoma
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Macro help to embed a SUM Function for tabulating the sum of a column or row

    John, the second example is giving me just what I'm looking for. I have a lot to learn about the syntax but this gets me going the right direction. I really appreciate your time and assistance. Thanks !

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro help to embed a SUM Function for tabulating the sum of a column or row

    You're welcome. Glad to help out, and thanks for the feedback. Please comply with Forum Rule No. 9.

    9. Acknowledge the responses you receive, good or bad. If your problem is solved, please say so clearly, and mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved". Or click the Edit button on your first post in the thread, Click Go Advanced, select [SOLVED] from the Prefix dropdown, then click Save Changes. If more than two days have elapsed, the Dropdown option or Edit button will not appear -- ask a moderator to mark it.

  7. #7
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro help to embed a SUM Function for tabulating the sum of a column or row

    Sorry, I didn't notice you already marked the thread as solved. Ignore Post #6.

+ 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