subtotal problem

thoomas

New Member
Joined
Apr 5, 2004
Messages
15
I'm still struggling with the subtotal macro. I would like to copy the result of the subtotal from the bottom of the previous page onto the top of the new page, but not into the column where SUM() function exists, but into the column just BEFORE that SUM() column. Is it possible? I tried to do this by myself, but as far as I'm complete newbie in coding, I was not successful. Thanx for any help...In the following code (credits go to btadams!) I have the situation where the code copies the subtotal into the SUM column (F), but I need to copy it into the column E. How should one modify that code?

Code:
Private Sub InsertSubTotal(RowIndex As Long, PreviousPageBreak As Long, InsertNewRows As Boolean, LabelText As String) 
' contains all editing necessary for each subtotal at the bottom of each page 
' customization is necessary depending on the subtotals you want to add 
Const RowsToInsert As Long = 3 
Dim i As Long, TargetRow As Long 
    TargetRow = RowIndex 
    If InsertNewRows Then ' not the last subtotal 
        For i = 1 To 2 * RowsToInsert 
            Rows(RowIndex - RowsToInsert).Insert 
        Next i 
        TargetRow = RowIndex - RowsToInsert 
    End If 
    If PreviousPageBreak < 1 Then PreviousPageBreak = 1 
    ' insert the necessary subtotal formulas here: 
    ' ADD ONE BLANK ROW BEFORE SUB-TOTAL 
    Cells(TargetRow + 1, 1).Formula = LabelText 
    With Cells(TargetRow + 1, 6) 
        .Formula = "=subtotal(9,r[-" & TargetRow - PreviousPageBreak & "]c:r[-1]c)" 
        .NumberFormat = .Offset(-2, 0).NumberFormat 
    End With 
    Range(Cells(TargetRow + 1, 1), Cells(TargetRow, 6)).Font.Bold = True 
    Range(Cells(TargetRow + 1, 1), Cells(TargetRow, 6)).Copy 
    Range(Cells(TargetRow + 4, 1), Cells(TargetRow + 3, 6)).PasteSpecial (xlPasteValues) 
    Range(Cells(TargetRow + 4, 1), Cells(TargetRow + 3, 6)).Font.Bold = True 
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
thoomas said:
...I have the situation where the code copies the subtotal into the SUM column (F), but I need to copy it into the column E. How should one modify that code?

Code:
Private Sub InsertSubTotal(RowIndex As Long, PreviousPageBreak As Long, InsertNewRows As Boolean, LabelText As String) 
' contains all editing necessary for each subtotal at the bottom of each page 
' customization is necessary depending on the subtotals you want to add 
'snip
    Range(Cells(TargetRow + 1, 1), Cells(TargetRow, 6)).Font.Bold = True 
    Range(Cells(TargetRow + 1, 1), Cells(TargetRow, 6)).Copy 
    Range(Cells(TargetRow + 4, 1), Cells(TargetRow + 3, 6)).PasteSpecial (xlPasteValues) 
    Range(Cells(TargetRow + 4, 1), Cells(TargetRow + 3, 6)).Font.Bold = True 
End Sub
Hi thoomas, where it says Cells (TargetRow, 6) make that Cells(TargetRow, 5)

Denis
 
Upvote 0
Thanks for your reply...But your solution does not work. I wonder what should be changed on the line where Range(Cells(TargetRow +4, 1...) is?
 
Upvote 0
Oops -- missed one.
This ... With Cells(TargetRow + 1, 6)
Should be... With Cells(TargetRow + 1, 5)

Denis
 
Upvote 0
Could you post a sample of your worksheet? I'm having trouble visualising the layout. (Use Colo's HYML Maker -- links on the Excel board's Index page)

Denis
 
Upvote 0
It works now this way:
Code:
Private Sub InsertSubTotal(RowIndex As Long, PreviousPageBreak As Long, InsertNewRows As Boolean, LabelText As String)
' contains all editing necessary for each subtotal at the bottom of each page
' customization is necessary depending on the subtotals you want to add
Const RowsToInsert As Long = 2
Dim i As Long, TargetRow As Long
    TargetRow = RowIndex
    If InsertNewRows Then ' not the last subtotal
        For i = 1 To 2 * RowsToInsert
            Rows(RowIndex - RowsToInsert).Insert
        Next i
        TargetRow = RowIndex - RowsToInsert
    End If
    If PreviousPageBreak < 1 Then PreviousPageBreak = 1
    ' insert the necessary subtotal formulas here:
    ' ADD ONE BLANK ROW BEFORE SUB-TOTAL
    Cells(TargetRow + 1, 4).Formula = LabelText
    With Cells(TargetRow + 1, 6)
        .Formula = "=subtotal(9,r[-" & TargetRow - PreviousPageBreak & "]c:r[-1]c)"
        .NumberFormat = .Offset(-2, 0).NumberFormat
    End With
    Range(Cells(TargetRow + 1, 1), Cells(TargetRow, 6)).Font.Bold = True
    Range(Cells(TargetRow + 1, 6), Cells(TargetRow, 6)).Copy
    Range(Cells(TargetRow + 1, 5), Cells(TargetRow + 2, 5)).PasteSpecial (xlPasteValues)
    Range(Cells(TargetRow + 1, 5), Cells(TargetRow + 2, 5)).Font.Bold = True
        
End Sub
The problem was that it didn't let me copy the entire row, so now I copied only SUM cell and it seems to work now. Thanks for your answers..
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top