Solved

Need help modifying an existing macros to allow for subtotals if there are 59 rows in the document

Posted on 2014-09-12
5
99 Views
Last Modified: 2014-09-15
Glen Ray assisted with a macro recently that I need help tweeking.  The macro adds subtotals and a grand total which can be applied and removed as additional data is entered using two different controls.

It seems that all is well when we have at least 60 rows of data, but if I have 59 or less, then the subtotal and grand total on the last page returns a circular reference error.  If I add data to row 60, everything works as expected.

Also, when only 59 rows are entered, the row formatting (borders) are not appearing at the top of the 2nd page - 1st row of data.

I have attached the file.
H--Documents-Bad-Debt-Log---Sylvia-Dorma
0
Comment
Question by:klitton7
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40320320
I found my last copy of the workbook I sent you and was able to replicate the error.  I've updated the code to correct it.  You should be able to select and copy the code from "Module2" (only the "Insert_Subotals" subroutine is changed) and put that in your current workbook.
Sub Insert_Subtotals()
    Dim r, i, x As Integer
    
    Range("A9").Select 'must start on row 9 to avoid false subtotal on top
    Do Until ActiveCell.Value = ""
        If (ActiveCell.Row - 6) Mod 28 = 0 Then
            'insert blank row
            ActiveCell.Offset(-1, 0).Select
            ActiveCell.EntireRow.Insert
            Range(ActiveCell, ActiveCell.Offset(0, 14)).Select
            For x = xlEdgeLeft To xlInsideHorizontal '7 to 12
                If x <> xlEdgeTop Then
                    With Selection.Borders(x)
                        .LineStyle = xlNone
                    End With
                End If
            Next x
            'insert page Total row
            ActiveCell.Offset(1, 0).Select
            ActiveCell.EntireRow.Insert
            ActiveCell.Value = "Total on this page"
            ActiveCell.Offset(0, 11).FormulaR1C1 = "=SUM(R[-27]C:R[-2]C)"
            ActiveCell.Offset(0, 12).FormulaR1C1 = "=SUM(R[-27]C:R[-2]C)"
            ActiveCell.Offset(0, 14).FormulaR1C1 = "=SUM(R[-27]C:R[-2]C)"
            Range(ActiveCell, ActiveCell.Offset(0, 14)).Select
            Selection.Font.Bold = True
            r = 0
        End If
        ActiveCell.Offset(1, 0).Select
        r = r + 1
    Loop
    
    'insert blank row
    ActiveCell.EntireRow.Insert
    'insert page Total row
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = "Total on this page"
    ActiveCell.Offset(0, 11).FormulaR1C1 = "=SUM(R[-" & r & "]C:R[-2]C)"
    ActiveCell.Offset(0, 12).FormulaR1C1 = "=SUM(R[-" & r & "]C:R[-2]C)"
    ActiveCell.Offset(0, 14).FormulaR1C1 = "=SUM(R[-" & r & "]C:R[-2]C)"
    ActiveCell.RowHeight = 19.5
    
    i = ActiveCell.Row - 6
    ActiveCell.Offset(1, 0).Value = "GRAND TOTALS"
    ActiveCell.Offset(1, 11).FormulaR1C1 = "=SUM(R[-" & i & "]C:R[-1]C)/2"
    ActiveCell.Offset(1, 12).FormulaR1C1 = "=SUM(R[-" & i & "]C:R[-1]C)/2"
    ActiveCell.Offset(1, 14).FormulaR1C1 = "=SUM(R[-" & i & "]C:R[-1]C)/2"
    ActiveCell.Offset(1, 0).RowHeight = 19.5
    
    Range(ActiveCell, ActiveCell.Offset(1, 14)).Select
    Selection.Font.Bold = True
   
    'copy formats for totals
    Range(ActiveCell.Offset(0, 11), ActiveCell.Offset(0, 14)).Copy
    Range(ActiveCell.Offset(0, 11), ActiveCell.Offset(1, 14)).PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    
    Range("A6").Select
    
End Sub

Open in new window


The changes are in lines 27 & 30 and the removal of the original code above line 33 (that's where the real error was occurring).

Updated example file attached.
EE-H-Documents-BadDebtLogMaster-2c.xls
0
 

Author Comment

by:klitton7
ID: 40323192
Thanks Glen - just one formatting issue to address if you don't mind.

In the file you sent me, I removed rows 60 and greater, then used the "insert total rows" control.

I notice that on page two, the borders do not appear on the top of the first row.  Can you adjust that for me if you don't mind?
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40323507
I fixed the formatting issue and updated the pagination logic also.  The last page should have split off one row earlier instead of pushing the page total and Grand total onto their own page.

Regards,
-Glenn
EE-H-Documents-BadDebtLogMaster-2d.xls
0
 

Author Comment

by:klitton7
ID: 40323744
Thanks Glenn!
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40323748
You're welcome.  Sorry for the confusion.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now