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.
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
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”
hi Gary, you could use Subtotal method and do that without VB.
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
it would be helpful to have a result sheet as well if you do not mind
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
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.
Subtotal by date first, then subtotal by type and make sure to uncheck the 'Replace existing subtotals' option.
Good luck.
OnErrorGoto0 got it right. Guess no coding necessary. Thanks,
Gary
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks