+ Reply to Thread
Results 1 to 9 of 9

Thread: Relative Summation formula

  1. #1
    Registered User
    Join Date
    05-13-2005
    Posts
    95

    Relative Summation formula

    I wrote a very short program that inserts a blank row at every change in date. I then move over to the column that contains the revenue figures for the records for that last date. I want to insert a summation formula that adds up the rows above the inserted row all the way up to the first blank row above. I recorded a macro that uses the sum (sigma) button in the Formula tab and it works fine, but just for the first set of rows because it did it absolutely, i think (just for that set or rows). When I run the program it doesn't add up the correct number of amounts for the desired set or rows (all with the same date). If I were manually inserting such a formula for each set of separated rows, it would correctly calculate the sum (up to the first blank cell above).

    how can I insert a sum formula into my code that always adds the amounts from the inserted (blank) row to the first blank row above?

    Thank you,


    Gary
    Last edited by nygwnj; 02-21-2012 at 09:09 PM.

  2. #2
    Valued Forum Contributor smuzoen's Avatar
    Join Date
    10-28-2011
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2003/2007/2010
    Posts
    359

    Re: Relative Summation formula

    You would need to upload a sample workbook - something like the following could be adapted to your needs however a sample workbook would make things much easier - if we assume the values are in column F you could find first row with a value then look for next blank row and insert the formula there. If you can adapt the following then great otherwise a sample workbook with no sensitive data/dummy workbook would make things easier. In the attached workbook run the macro "InsertSum"
    Option Explicit
    
    Sub insertSum()
    Dim fRow As Range, stRow As Long
    With Columns("F")
    Set fRow = .Find(what:="*", after:=.Cells(1, 1), LookIn:=xlFormulas)
    End With
    stRow = fRow.Row
    Set fRow = Range("F" & fRow.Row).Resize(Cells(Rows.Count, "F").End(xlUp).Row).SpecialCells _
         (xlCellTypeBlanks)
    Range("F" & fRow.Row).Formula = "=Sum(R[-1]C:R[-" & fRow.Row - stRow & "]C)"
    End Sub
    Attached Files Attached Files
    Hope this helps.
    Anthony
    Pack my box with five dozen liquor jugs
    PS: Remember to mark your questions as Solved once you are satisfied. Please rate the answer(s) by selecting the Star in the lower left next to the Triangle. It is appreciated

  3. #3
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Relative Summation formula

    hi Gary, you could use Subtotal method and do that without VB.

  4. #4
    Registered User
    Join Date
    05-13-2005
    Posts
    95

    Re: Relative Summation formula

    Thank you for your replies. I realized I need it not only sub-totaled at every change in date, but also at every change in tender type within date. I attached the file, with the insert_Sum sub smuzoen provided inserted into its own module and my code calling that sub. It wouldn't even compile at that sub call.

    the data is sorted so all amex charges are together per date. I need the insert_Sum sub modified so it also inserts the blank row at a change in tender type per new date, calculates the sub-total, then inserts another line right before the change in date, and adds the amex and v/mc sub-totals to get a grand total per date.

    If the sub-total method not requiring VBA can do this, that would be fine, but I think that can only operate on one level of "at every change in."

    Gary
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Relative Summation formula

    it would be helpful to have a result sheet as well if you do not mind

  6. #6
    Registered User
    Join Date
    05-13-2005
    Posts
    95

    Re: Relative Summation formula

    I guess watersev meant a second worksheet with the data while also keeping the original sheet. That is what I did with the most recent attachment - let me know if you want anything else.


    gary
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Relative Summation formula

    that's right Gary, I meant a sheet showing result you need to obtain for the original data provided. Your post #5 attachment just has another copy of your original data with no "required" result data layout.

  8. #8
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Relative Summation formula

    Subtotal by date first, then subtotal by type and make sure to uncheck the 'Replace existing subtotals' option.
    Good luck.

  9. #9
    Registered User
    Join Date
    05-13-2005
    Posts
    95

    Re: Relative Summation formula

    OnErrorGoto0 got it right. Guess no coding necessary. Thanks,


    Gary

+ 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.2.0