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
column DColumn I
11234
20
3
454213
50
60
73456
8
90

<tbody>
</tbody>


Thanks in advance
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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
Column DColumn I
11
22
30
44
50
6
70
88
9

<tbody>
</tbody>

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,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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