Removing Subtotals with a value of Zero and related data

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
145
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
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.
 

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
145
I am deleting several hundred subtotals. Ok. I will check the remaining formulae. Thanks!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
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
 

Forum statistics

Threads
1,082,300
Messages
5,364,381
Members
400,796
Latest member
vrcdesktop

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top