delete data if the two columns contain zero together

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
Hello
I have about 7000 rows . I search for way delete data contin zero or empty value for columns D,E together then should delete it.

orginal
DELETE2.xlsm
ABCDEF
1ITEMIDQTY1QTY2QTY3BALANCE
21TT/W-1 MM CLA1 23M-1 IT500500
32QQW-2 TH NM-1 CLA2 VBG L CHI0
43QQW-3 CV CLA3 TAI70033733
54QQW-4 M*12.5 CLA4 TR20012188
65QQW-5 CLA5 EG300300
76MMR12/100 AS-1000/1 TMR120012
87QQW-6 M230 TU11
98QQW-7 S** CLA7 US140011399
109QQW-8 CLA8 UK0
1110QQW-9 CLA9 N BR160011599
1211QQW-10 BN CLA10 IT80000800
1312QQW-11 LVD CH900900
1413BB12 QQW-12 CLA12 JA100011989
sheet1
Cell Formulas
RangeFormula
F2:F14F2=C2+D2-E2



result

DELETE2.xlsm
ABCDEF
1ITEMIDQTY1QTY2QTY3BALANCE
21QQW-3 CV CLA3 TAI70033733
32QQW-4 M*12.5 CLA4 TR20012188
43QQW-7 S** CLA7 US140011399
54QQW-9 CLA9 N BR160011599
65BB12 QQW-12 CLA12 JA100011989
sheet1
Cell Formulas
RangeFormula
F2:F6F2=C2+D2-E2
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Are you looking for a VBA solution or just a quick fix ?

For a quick fix just filter both D & E using a custom filter.
Put equals nothing / Blank for the 1st filter then
User OR
Put in equals 0 for the second filter
Repeat on the 2nd column.

Then select all the rows and delete rows

Remove filter and renumber remaining rows.

1663908663796.png
 
Upvote 0
I might not get time to do it today, if no one has done it for you by tomorrow, I can look at it then.
 
Upvote 0
Here VBA solution.
This code employ column ZZ (or any available column) to help to delete rows, then this column will be removed.
SQL:
Option Explicit
Sub delete()
Dim lr&
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("ZZ2:ZZ100000").ClearContents
Range("ZZ2:ZZ" & lr).Value = Evaluate("=1/(D2:D" & lr & "+E2:E" & lr & ")")
Range("ZZ2:ZZ" & lr).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.delete
Columns("ZZ").ClearContents
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("A2:A" & lr).Value = Evaluate("=ROW(2:" & lr & ")-1")
End Sub

P/S: it will not work, if positive in D and negative in E those make 0 in sum
might it not the actual case?
 
Upvote 0
@bebo021999 great !
might it not the actual case?
surely I don't have this case even that works . it stay the minus values without any problem.
las thing ,please I want keeping orginal data in sheet1 and result in sheet2
and where is the line search for empty or zero ,there is no condition in your code?
 
Upvote 0
Try this?

VBA Code:
Option Explicit
Sub Hasson()
    Application.ScreenUpdating = False
    Dim LRow As Long, LCol As Long, i As Long
    Dim a, b
    LRow = Cells.Find("*", , xlFormulas, , 1, 2).Row
    LCol = Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    a = Range("D2:E" & LRow)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a)
        If a(i, 1) + a(i, 2) = 0 Then b(i, 1) = 1
    Next i
    
    Cells(2, LCol).Resize(UBound(a)) = b
    i = WorksheetFunction.Sum(Columns(LCol))
    Range(Cells(2, 1), Cells(LRow, LCol)).Sort Key1:=Cells(2, LCol), order1:=1, Header:=2
    If i > 0 Then Cells(2, LCol).Resize(i).EntireRow.Delete

    LRow = Cells.Find("*", , xlFormulas, , 1, 2).Row
    With Range("A2:A" & LRow)
        .Formula = "=Row()-1"
        .Value = .Value
    End With
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@bebo021999
las thing ,please I want keeping orginal data in sheet1 and result in sheet2
and where is the line search for empty or zero ,there is no condition in your code?
If current sheet was sheet1, just add 1 line in top of code, to dupplicate new sheet, sheet2
VBA Code:
Sheets("Sheet1").Copy after:=Sheets(Sheets.Count)
If in helper column from row 2, there is formula:
=1/(D2+E2)
you can see "#DIV/0" where D2+E2=0
and manually, you can filter this column then delete rows.

In code, with no loop, then helper column could be populated, at once, in bulk like this:
Code:
Range("ZZ2:ZZ" & lr).Value = Evaluate("=1/(D2:D" & lr & "+E2:E" & lr & ")")


VBA Code:
Option Explicit
Sub delete()
Dim lr&
Sheets("Sheet1").Copy after:=Sheets(Sheets.Count)
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("ZZ2:ZZ100000").ClearContents
Range("ZZ2:ZZ" & lr).Value = Evaluate("=1/(D2:D" & lr & "+E2:E" & lr & ")")
Range("ZZ2:ZZ" & lr).SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.delete
Columns("ZZ").ClearContents
lr = Cells(Rows.Count, "B").End(xlUp).Row
Range("A2:A" & lr).Value = Evaluate("=ROW(2:" & lr & ")-1")
End Sub
 
Upvote 0
Solution
I want keeping orginal data in sheet1 and result in sheet2
Adjustment to code in post #8 based on above comment.

VBA Code:
Option Explicit
Sub Hasson_V2()
    Application.ScreenUpdating = False
    Dim LRow As Long, LCol As Long, i As Long
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    Dim a, b
    
    ws2.UsedRange.Clear
    With ws1.Range("A1").CurrentRegion
        .Copy
        ws2.Range("A1").PasteSpecial xlPasteAll
        ws2.Range("A1").PasteSpecial xlPasteColumnWidths
        .Copy ws2.Range("A1")
        Application.CutCopyMode = False
    End With
    
    LRow = ws2.Cells.Find("*", , xlFormulas, , 1, 2).Row
    LCol = ws2.Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    
    a = ws2.Range("D2:E" & LRow)
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a)
        If a(i, 1) + a(i, 2) = 0 Then b(i, 1) = 1
    Next i
    
    ws2.Cells(2, LCol).Resize(UBound(a)) = b
    i = WorksheetFunction.Sum(Columns(LCol))
    ws2.Range(ws2.Cells(2, 1), ws2.Cells(LRow, LCol)).Sort Key1:=ws2.Cells(2, LCol), order1:=1, Header:=2
    If i > 0 Then ws2.Cells(2, LCol).Resize(i).EntireRow.Delete

    LRow = ws2.Cells.Find("*", , xlFormulas, , 1, 2).Row
    With ws2.Range("A2:A" & LRow)
        .Formula = "=Row()-1"
        .Value = .Value
    End With
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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