Macro to delete all rows that sum to zero

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a macro below to delete all rows there the debits and credits sum to zero


However, it also delete values that do not sum to zero


Code:
 Sub DeleteRows_SummingtoZero()
 Dim rw As Long, i As Long
 rw = Cells(Rows.Count, 1).End(xlUp).Row
 For i = rw To 2 Step -1
 If Application.Sum(Cells(i, 9)) = 0 Then
 Rows(i).Delete
 End If
 Next

 End Sub


See link below


I have also posted on Macro to delete Rows where Values add up to zero In Col H (excelforum.com)





It would be appreciated if someone could kindly assist me
 
hi Joe

You code works if the Debits and credits are beneath each other. If I could get the debit say 1000 and correspoding credit -1000 below each otherr, this will work
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Sorry, I have been away for over a week.
If you still need help with this, please explain in more detail what you are still looking for, using images to illustrate what you mean.
 
Upvote 0
HI Joe

Just to let you know that I have made use of a formula to return True or False for contra entries
 
Upvote 0
OK, can you re-mark the actual post that had the solution to the original question asked in this thread?
The one you just marked does not have any solution.
 
Upvote 0
Hi Joe


I just tested your code on the data below and cannot get i to delete the items where thee is a debit in Col H as well as a corresponding Credit

as can be seen H2 & H3 Contra and should be deleted

Kindly test and advise


Delete Debit value where Corresponing credit Value.xlsm
ABCDEFGH
1ReferenceSourceRunA/CDateDebitCreditBalance
20053157Purchase Ordering07/06/2021135601356
356Sales Ledger22/06/202101356-1356
4662PPurchase Ledger30/06/2021103.50103.5
5BRTW MAY21Purchase Ledger14/06/20210461.49-461.49
6582KNominal Ledger29/06/20216044.406044.4
7598GNominal Ledger30/06/20210103.6-103.6
853KPurchase Ledger30/06/20216232.606232.6
Sheet1
Cell Formulas
RangeFormula
H2:H8H2=+F2-G2
 
Upvote 0
I ran the code against the sample you posted, and it deleted the first two data lines for me.
Does it not do that for you?

If it does not delete those two rows, then you probabaly have a floating-point precision issue going on.
That can be easily remedied by changing your formulas in column H like this:
Excel Formula:
=ROUND(F2-G2,2)
 
Upvote 0
Thanks for the formula. Code runs perfectly now
 
Upvote 0
You are welcome.

Since the code that was posted originally did indeed work once we fixed up the data/formula, I have re-marked that as the solution, as that is the reply that most accurately answers the original question you posted (the latest fix to your data does not answer the original question asked).
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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