Code to calculate totals needs to take into account previous totals


Well-known Member
Aug 14, 2018
I have a spreadsheet relating to various periods on one sheet. There is a button to add a period and when it is pressed it copies the 20 rows of calculations and pastes them below the bottom row.

Each period has 2 tables for calculations and totals. At the bottom of the periods, I want a grand total. The grand total is to appear under the bottom instance of the tables and totals, depending on how many periods have been added. For the first period:
  • H32 contains the total for the period
  • H33 contains the grand total
When the Addrows code is run, I need the new grand total to be a sum of the cell directly above it, which is the total for the last period and the cell that is 20 rows above it in the same row, which is the previous grand total.

This is my code. It worked fine until I tried to add the above feature with the last line of code.

VBA Code:
Sub AddRows()
    Dim Total As Range
    Dim WS As Worksheet
    Dim Sh As Shape, NewShape As Shape
    Dim TTop As Long
    Dim TLeft As Long

    Set WS = ThisWorkbook.Worksheets("ACA_Quoting")

    For Each Sh In WS.Shapes
        Select Case Sh.Name
        Case "cmdAddRatio", "cmdCustomSign", "cmdGsign", "cmdNoSign", "cmdSaveToPdf"
            Sh.IncrementTop 472.5
        Case "txtMain"                                'name your first textbox, the one you want to move,  to something unique. I used "txtMain"
            TTop = Sh.Top                             'record position
            TLeft = Sh.Left
            Sh.IncrementTop 472.5                     'move it
            Sh.Copy                                   'make a copy
            Set NewShape = WS.Shapes(WS.Shapes.Count)    'pasted textbox is the last shape
            With NewShape
                .Top = TTop                           'move the copy to the previous position of txtMain
                .Left = TLeft
            End With
        End Select
    Next Sh
    With WS
        .Range("A8:V33").Copy .Cells(.Rows.Count, "A").End(xlUp).Offset(10, 0) 'Pastes a copy of the table below current table between the bottom of the table and the totals
        .Cells(.Rows.Count, "H").End(xlUp).Formula = .Cells(.Rows.Count, "H").End(xlUp).Formula & "+" & .Cells(.Rows.Count, "H").End(xlUp).Offset(10, 0).Formula
    End With
End Sub

When I try and run this code I get the error Application defined or object defined error. How do I fix it so it inserts the correct formula?


Some videos you may like

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Michael M

Well-known Member
Oct 27, 2005
Office Version
  1. 2013
  1. Windows
I might be missing the point, but why don't you simply sum the entire column and then divide by 2


Well-known Member
Aug 14, 2018
As each period is different. I am only copying the layout. If there is any data in the tables, it will be changed.

Watch MrExcel Video

Forum statistics

Latest member