delete row (range) if Zero found

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,602
Office Version
  1. 365
Platform
  1. Windows
Hi all

NAMEPROFITWLWIN %
DAVE34.75101
TIM0000
KEVIN0000
PETER0000
ALAN0000
PAUL-9.84010



Simple enough but can't get to VBA to work
Simply look through range B3:B8 and if a Zero is found then remove (Delete and move up) all data in corresponding rows A:E
so for example if a zero is found (as shown in B4) then A4:E4 is deleted (and moved up)

TIA
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
There's a number of different ways you could do this; here's one:

VBA Code:
Sub ZapZeroes()
    With ActiveSheet
        If .AutoFilterMode Then .AutoFilter.ShowAllData
        With .Range("A1").CurrentRegion
            .AutoFilter 2, "0"
            If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
                .Offset(1).Resize(, 5).Delete shift:=xlUp
            End If
            .AutoFilter
        End With
    End With
End Sub
 
Upvote 0
There's a number of different ways you could do this; here's one:

VBA Code:
Sub ZapZeroes()
    With ActiveSheet
        If .AutoFilterMode Then .AutoFilter.ShowAllData
        With .Range("A1").CurrentRegion
            .AutoFilter 2, "0"
            If .SpecialCells(xlCellTypeVisible).Address <> .Rows(1).Address Then
                .Offset(1).Resize(, 5).Delete shift:=xlUp
            End If
            .AutoFilter
        End With
    End With
End Sub
Cheers Kevin for the reply
Didn't think it would cause an issue but in columns AB I have a pivot so I'm getting an error with this line of the code
VBA Code:
.Offset(1).Resize(, 5).Delete shift:=xlUp
as the removal of the zero data will affect the pivot (the error is saying)
🤔🤔
 
Upvote 0
Understood. Give this a try instead and see if it works for you:
VBA Code:
Sub ZapZeroes2()
    Dim LRow As Long, i As Long
    LRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = LRow To 2 Step -1
        If Cells(i, 2) = "0" Then
            Range(Cells(i, 1), Cells(i, 5)).Delete Shift:=xlUp
        End If
    Next i
End Sub
 
Upvote 0
Understood. Give this a try instead and see if it works for you:
VBA Code:
Sub ZapZeroes2()
    Dim LRow As Long, i As Long
    LRow = Cells(Rows.Count, 1).End(xlUp).Row
   
    For i = LRow To 2 Step -1
        If Cells(i, 2) = "0" Then
            Range(Cells(i, 1), Cells(i, 5)).Delete Shift:=xlUp
        End If
    Next i
End Sub


OK, getting there
If I were to have the table of data starting in cell O1 (instead of A1) would I change your code to below...

VBA Code:
Sub ZapZeroes2()
    Dim LRow As Long, i As Long
    LRow = Cells(Rows.Count, 15).End(xlUp).Row
    
    For i = LRow To 2 Step -1
        If Cells(i, 16) = "0" Then
            Range(Cells(i, 15), Cells(i, 5)).Delete Shift:=xlUp
        End If
    Next i
End Sub
 
Upvote 0
Almost perfect!
Rich (BB code):
Sub ZapZeroes2()
    Dim LRow As Long, i As Long
    LRow = Cells(Rows.Count, 15).End(xlUp).Row
    
    For i = LRow To 2 Step -1
        If Cells(i, 16) = "0" Then
            Range(Cells(i, 15), Cells(i, 19)).Delete Shift:=xlUp
        End If
    Next i
End Sub
 
Upvote 1
Solution
Almost perfect!
Rich (BB code):
Sub ZapZeroes2()
    Dim LRow As Long, i As Long
    LRow = Cells(Rows.Count, 15).End(xlUp).Row
   
    For i = LRow To 2 Step -1
        If Cells(i, 16) = "0" Then
            Range(Cells(i, 15), Cells(i, 19)).Delete Shift:=xlUp
        End If
    Next i
End Sub


Fantastic - perfect

Thanks a lot
Have a great weekend
 
Upvote 0

Forum statistics

Threads
1,215,284
Messages
6,124,059
Members
449,139
Latest member
sramesh1024

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