• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

NEED HELP - Fix Macro Automatically SUM upper data when entering new data

depchai

New Member
Hi everybody.
I have a macro, it will automatically add a row and calculate SUM all the data of upper rows when I entering new data.

But if I add or delete any value in Quantity column from any Group, the SUM row will not correct because it does not plus or minus that value (please view attached file)

PN9LBOs.png



So I want this macro will calculate exactly when I add more or delete any value.
Please help me fix this macro !!! Thank you!!!! And sorry for my bad English.

Here is my macro
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim so As String
Dim dg As Long
dg = Target.Row
Application.EnableEvents = False
If Target.Column = 1 And Target.Text <> "" Then
If Target.Row <= 2 Then Exit Sub
For i = Target.Row - 1 To 1 Step -1
If Sheet1.Cells(i, 1) <> "" Then
so = Sheet1.Cells(i, 1)
Exit For
End If
Next
Range(Target.Address, Target.Offset(0, 3).Address).Insert Shift:=xlDown
Sheet1.Cells(dg, 1).EntireRow.Interior.ColorIndex = 35
Sheet1.Cells(dg, 1).EntireRow.Font.Bold = True
Sheet1.Cells(dg, 1).EntireRow.Font.Size = 16
Sheet1.Cells(dg, 1) = "SUM"
Sheet1.Cells(dg, 3).Formula = Application.WorksheetFunction.Sum(Range(Sheet1.Cells(dg - 1, 3), Sheet1.Cells(i, 3)))
Sheet1.Cells(dg + 1, 2).Select
End If
Application.EnableEvents = True
End Sub
 

Attachments

  • Auto_Sum_Upper.xlsm
    17.5 KB · Views: 4
  • PN9LBOs.png
    PN9LBOs.png
    76.9 KB · Views: 0
I'd highly recommend using built in "Subtotal" instead of using custom code.
upload_2016-4-27_15-57-28.png
Only caveat being that you need your data sorted by Group and Group name needs to be repeated at each row. See attached sample.
 

Attachments

  • SubTotal_Sample.xlsx
    12.7 KB · Views: 2
I'd highly recommend using built in "Subtotal" instead of using custom code.
View attachment 30298
Only caveat being that you need your data sorted by Group and Group name needs to be repeated at each row. See attached sample.

Yeah, I know "Subtotal" function, but my data is large and I can't handle it by using "Subtotal" everytime I entering new data. So I want to use that macro to calculate it automatically.

Can you help me fix this code?
 
I'm confused. Once set up. You don't need to do anything. You can insert rows and no need to update formula or reapply grouping.

If you don't want calculation every time you update data, then just set calculation mode to manual and recalculate at end of your process.

I really wouldn't recommend VBA in this case.
 
Back
Top