Excel 2010 code to calculate totals does not work when auto-filling

I was helped a few months ago by an expert. He provided me code that would calculate totals on a spreadsheet without any formulas needed. This works great as long as someone enters a value into a cell manually or copies and pastes a value into a cell, but if the cells are filled using the auto-fill drag method, the totals do not calculate. You will notice this in row 41 of the attached sheet. There is an entry of $25,000,00 in cell I41 that was manually typed, and the total below is working, but if you look to the right at cells K41 thru P41, which were populated with auto fill, the totals do not populate. If you type over any of those $25,000 entries, you will see the total populate. Can I make this work no matter how the user gets the value in the cell?
thx experts...BobR
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Let me see what I can do.
Martin LissOlder than dirtCommented:
Try this

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lngFirstRow As Long
    Dim lngLastRow As Long
    Dim lngRow As Long
    Dim lngColumn As Long
    Dim lngTotalsCol As Long
    Dim curRevenue() As Currency

    Const MATERIAL_COL = 3
    Const REVENUE_COL = 4
    Const FIRST_MONTH_COL = 5
    Const GRAND_TOTAL_COL = 2
    ' check to see if...
    ' A Total in a Revenue line is being changed, or
    ' A Total in a BackLog line is being changed, Or
    ' A Grand Total in a Revenue line is being changed, or
    ' A Grand Total in a BackLog line is being changed
    If Cells(Target.Row, REVENUE_COL) = "Revenue" And Cells(Target.Row, MATERIAL_COL) = "Total" Or _
       Cells(Target.Row, REVENUE_COL) = "BackLog" And Cells(Target.Row - 1, MATERIAL_COL) = "Total" Or _
       Cells(Target.Row, REVENUE_COL) = "Revenue" And Cells(Target.Row, GRAND_TOTAL_COL) = "Total" Or _
       Cells(Target.Row, REVENUE_COL) = "BackLog" And Cells(Target.Row - 1, GRAND_TOTAL_COL) = "Total" Then
        Application.EnableEvents = False
        Application.EnableEvents = True

        MsgBox "Changes to Totals are not allowed", vbOKOnly + vbExclamation
        Exit Sub
    End If

    lngTotalsCol = Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column
    If Not Intersect(Target, Range(UsedRange.Address)) Is Nothing Then
        ' The Cells() property is used to specify a particular cell on the worksheet.
        ' The syntax is Cell(Row number, column number). You can think of them as
        ' the coordinates of the cell. In this line the row coordinate is Target.Row
        ' which is the row number of the changed revenue cell, and the column coordinate
        ' is the REVENUE_COL which is defined as a constant of 4
        If Cells(Target.Row, REVENUE_COL) = "Revenue" Then
            Application.EnableEvents = False
            ' Find the first row for the customer that contains
            ' the changed reveue amount
            For lngRow = Target.Row To 2 Step -1
                If Cells(lngRow, MATERIAL_COL) = "Total" Then
                    lngFirstRow = lngRow + 2
                    Exit For
                End If
            If lngFirstRow = 0 Then
                lngFirstRow = 2
            End If
            ' Find the last row for the customer
            For lngRow = Target.Row To UsedRange.Rows.Count
                If Cells(lngRow, MATERIAL_COL) = "Total" Then
                    lngLastRow = lngRow
                    Exit For
                End If
            Application.ScreenUpdating = False
            ' Add up the revenue in the changed row and put it in the "Total" column
            Cells(Target.Row, lngTotalsCol) = _
                    Application.WorksheetFunction.Sum(Range(Cells(Target.Row, FIRST_MONTH_COL), Cells(Target.Row, lngTotalsCol - 1)))
            ReDim curRevenue(Target.Column To Target.Column + Target.Columns.Count - 1)
            For lngColumn = Target.Column To Target.Column + Target.Columns.Count - 1
                For lngRow = lngFirstRow To lngLastRow
                    If Cells(lngRow, MATERIAL_COL) = "Total" Then
                        ' Put the sum of the customer's revenue in the total for the month
                        ' for that customer
                        Cells(lngRow, lngColumn) = curRevenue(lngColumn)
                        ' Add up the revenue in the "Total" row and put it in the "Total" column
                        Cells(lngRow, lngTotalsCol) = _
                                Application.WorksheetFunction.Sum(Range(Cells(lngRow, FIRST_MONTH_COL), Cells(lngRow, lngTotalsCol - 1)))
                        If Cells(lngRow, REVENUE_COL) = "Revenue" Then
                            curRevenue(lngColumn) = curRevenue(lngColumn) + Val(Cells(lngRow, lngColumn))
                        End If
                    End If
                ' Update the revenue totals in the footer for the month
                ' that was changed
                curRevenue(lngColumn) = 0
                ' 3 is the first data row and UsedRange.Rows.Count returns
                ' the last used row on the sheet
                For lngRow = 3 To ActiveSheet.UsedRange.Rows.Count - 1
                    If lngRow = ActiveSheet.UsedRange.Rows.Count - 1 Then
                        Cells(lngRow, lngColumn) = curRevenue(lngColumn)
                        If Cells(lngRow, REVENUE_COL) = "Revenue" And Cells(lngRow, MATERIAL_COL) = "Total" Then
                            ' Sum the revenue values. Val() returns zero when the value is a space.
                            curRevenue(lngColumn) = curRevenue(lngColumn) + Val(Cells(lngRow, lngColumn))
                        End If
                    End If
                ' Update the revenue grand total in the footer. This
                ' could be done via a formula
                Cells(ActiveSheet.UsedRange.Rows.Count - 1, lngTotalsCol) = _
                        Application.WorksheetFunction.Sum(Range(Cells(ActiveSheet.UsedRange.Rows.Count - 1, FIRST_MONTH_COL), _
                                                          Cells(ActiveSheet.UsedRange.Rows.Count - 1, lngTotalsCol - 1)))

            Application.ScreenUpdating = True
            Application.EnableEvents = True
        End If
    End If

Open in new window

End Sub

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bobrossi56Author Commented:
Martin...once again you have saved the day, I tested the new code and it woks great. BUT...I am a tad confused on where I need to put it to make it all work with your other master.xlsx and salesperson.xltm. All I know is I do the extract from JReport, then run your master.xlsx file and it does all the magic for me. I know I have to replace existing code with this new code, just not sure where, in the mater.xlsx or salesperson.xltm?
thx Martin...
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Martin LissOlder than dirtCommented:
If I remember correctly it would go in salesperson.xltm (the template) so that any time you create a new report for a salesperson the code will be there.
bobrossi56Author Commented:
OK, makes sense, I'll give that a try and so another "slice and dice" and try the autofill to make sure I get totals.
thanks again Martin
bobrossi56Author Commented:
Perfect, top notch work....thanks so much
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.