delete row if the value in column J contains 0

GirishDhruva

Active Member
Joined
Mar 26, 2019
Messages
308
Hi Everyone,
Here i am trying to delete entire row if the value in column 'I' is 0.
The below code works but it also deletes the empty spaces that are present in column'I' where that shouldn't happen
Can anyone suggest/guide me what changes i should make ??????

Code:
    last = Cells(Rows.Count, "D").End(xlUp).Row 
    For i = last To 4 Step -1
        If Cells(i, "I").Value = 0 Then
                Cells(i, "I").EntireRow.Delete
        End If
   Next i

like
it should delete only the the value with 0(entire row), not the cell that contains empty spaces
[TABLE="width: 500"]
<tbody>[TR]
[TD]column D[/TD]
[TD]Column I[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1234[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]54213[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]3456[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]0[/TD]
[/TR]
</tbody>[/TABLE]


Thanks in advance
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:
Code:
    last = Cells(Rows.Count, "D").End(xlUp).Row 
    For i = last To 4 Step -1
        If (Cells(i, "I").Value = 0) And (Len(Cells(i, "I") > 0) Then
            Rows(i).Delete
        End If
   Next i
 
Last edited:
Upvote 0
@Joe4 thanks for your effort but this also doesn't work as i needed.
My main requirement is, the values in column 'I' which contains 0 should be deleted and the empty cells in column'I' shouldn't be deleted

Thanks in advance
 
Last edited:
Upvote 0
Sorry, I was missing a parentheses in my code. It should be:
Code:
    last = Cells(Rows.Count, "D").End(xlUp).Row
    For i = last To 4 Step -1
        If (Cells(i, "I").Value = 0) And (Len(Cells(i, "I")) > 0) Then
            Rows(i).Delete
        End If
   Next i
 
Upvote 0
Sorry, I was missing a parentheses in my code. It should be:
Code:
    last = Cells(Rows.Count, "D").End(xlUp).Row
    For i = last To 4 Step -1
        If (Cells(i, "I").Value = 0) And (Len(Cells(i, "I")) > 0) Then
            Rows(i).Delete
        End If
   Next i

@Joe4 your solution also deletes blank spaces in column'I' where that shouldn't happen

It should only delete the rows which contains 0 in column'I'
 
Upvote 0
That tells me that your cells are not really empty, that there is something (like spaces in there).
I was kind of confused when you said "empty spaces". A cell can either be empty, or have spaces. If it has spaces, it is not empty.
I was assuming that the cells were empty, but it sounds like you have spaces in them.

If that is the case, and they are just normal spaces, then the following update should work:
Code:
    last = Cells(Rows.Count, "D").End(xlUp).Row
    For i = last To 4 Step -1
        If (Cells(i, "I").Value = 0) And (Len(Trim(Cells(i, "I"))) > 0) Then
            Rows(i).Delete
        End If
   Next i
If that does NOT work, then you do not have normal spaces in there, and we will need to identify exactly what is in those cells to see what we are dealing with.

Find one of these cells, and enter these formulas in some blank cells and let me know what they return.
Let's say that the cell is I4, then use these formulas:
=LEN(I4)
=CODE(LEFT(I4,1))

and tell me what they return.
 
Upvote 0
Hi GirishDhruva,

Not much different but this works for me:

Code:
Option Explicit
Sub Macro1()
    
    Dim last As Long
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    last = Cells(Rows.Count, "I").End(xlUp).Row
    
    For i = last To 1 Step -1
        If Val(Range("I" & i)) = 0 Then
            Rows(i).Delete
        End If
   Next i
   
   Application.ScreenUpdating = True
   
End Sub

Regards,

Robert
 
Upvote 0
Thanks for your effort @Trebor76 but your code is also deleting blank/empty cells which are present in column'I', but that shouldn't happen only the cells that contains zero should be deleted

I tried with the below data
[TABLE="width: 500"]
<tbody>[TR]
[TD]Column D[/TD]
[TD]Column I[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

Hi GirishDhruva,

Not much different but this works for me:

Code:
Option Explicit
Sub Macro1()
    
    Dim last As Long
    Dim i As Long
    
    Application.ScreenUpdating = False
    
    last = Cells(Rows.Count, "I").End(xlUp).Row
    
    For i = last To 1 Step -1
        If Val(Range("I" & i)) = 0 Then
            Rows(i).Delete
        End If
   Next i
   
   Application.ScreenUpdating = True
   
End Sub

Regards,

Robert
 
Last edited:
Upvote 0
Not sure as it worked for me :confused:

Can you use a hosting site like www.box.com and provide a link to your workbook so we can see what's happening?

Thanks,

Robert
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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