dpaton05
Well-known Member
- Joined
- Aug 14, 2018
- Messages
- 2,375
- Office Version
- 365
- 2016
- Platform
- Windows
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:
This is my code. It worked fine until I tried to add the above feature with the last line of code.
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?
Thanks
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
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
WS.Paste
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?
Thanks