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:
For =LEN(I4) o/p is 0 but
For =CODE(LEFT(I4,1)) its throwing me an #Value ! error
That confirms that the cell (I4) is blank (empty), so it should NOT be deleted.
The way I wrote the code, it will ONLY delete rows where column I is NOT blank/empty.

So if it is not working for you, then I feel that there is some important detail that you are not telling us.
- Is column I part of merged cells?
- Are these hard-coded values or formulas in column I?
- Do you have any other VBA code running (there may be other code that is deleting that)?

If you run the following code (and ONLY that code) on the data sample you posted in post 9:
Code:
Sub MyDeleteRows()

    Dim last As Long
    
    Application.ScreenUpdating = False

    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
   
    Application.ScreenUpdating = True
   
End Sub
you end up with the following:
Code:
Column D     Column I
  1              1
  2              2
  4              4
  6
  8              8
  9
which appears to correctly do what you ask (does NOT delete values in column D where it is blank).
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Yes you are right @Joe4 , i had another VBA code which was deleting those empty cells, as i removed that code your code was successfully executed as i required ,sorry for confusing you and thank you so much for your effort friends
 
Last edited:
Upvote 0
You are welcome.

I figured that it had to be something like that. The code is pretty straightforward, and should have worked, unless there was some interference.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
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