Removing Subtotals with a value of Zero and related data

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
158
I have a macro to remove all subtotals that equal zero along with the data making up the total; however it appears to be deleting more data than it should.

For example, I know the outstanding total should be $157,542 but when I run the macro to remove the zero subtotals my outstanding balance changes to $156,606.

I can't understand what I am doing wrong. There are no blank cells or cells that contain zeros in the data that create the subtotal.

Sub Remove_Zer_SubTotals()


'Set Variables
Dim Ar As Areas
Dim Rng As Range

Dim Previous As Worksheet
Set OST = ThisWorkbook.Sheets("OS Transactions")


'Turn off printing and screen updating to speed up macro
With Application
.ScreenUpdating = False
.PrintCommunication = False
End With

'Delete all subtotals with a zero balance and associated data above the subtotal

Set Ar = Range("O2", Range("O" & Rows.Count).End(xlUp)).SpecialCells(xlConstants).Areas
For Each Rng In Ar
If Round(Rng.Offset(Rng.Count).Resize(1).Value, 0) = 0 Then Rng.Resize(Rng.Count + 1).EntireRow.Delete
Next Rng

'Turn on printing and screen updating to speed up macro
With Application
.ScreenUpdating = True
.PrintCommunication = True
End With

' Have message box appear to know the macro has completed running
MsgBox "Done!!!"

End Sub

Can you help me?
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I can't see anything obviously wrong with the code.
Roughly how many subtotals are you deleting?
Also check that after deleting blocks of data your remaining formulae are still looking at the correct ranges.
 
Upvote 0
As you are using "Round" you may get some discrepancies in the before & after total.
If you do it like this, it will tell you the value of the cells you are deleting
Code:
Dim Tot As Double
For Each rng In Ar
If Round(rng.Offset(rng.Count).Resize(1).Value, 0) = 0 Then
   Tot = Tot + rng.Offset(rng.Count).Resize(1).Value
   rng.Resize(rng.Count + 1).EntireRow.Delete
End If
Next rng
MsgBox Tot
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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