Delete Row if Cell has a 0 value

gottimd

Well-known Member
Joined
Jul 29, 2002
Messages
501
I want the Macro to look in the column c6:c224, and if the cell has a zero value it deletes that row, but cycle through all the cells in c6 through c224 in doing that, not just to stop after the first one.

Can anyone help out?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
There may be a better way, but this seemed to do the trick for me:

Code:
Option Explicit

Sub DeleteRows()

Dim ChkRange As Range
Set ChkRange = Range("C6:C224")

Dim cell As Range

For Each cell In ChkRange
    If cell = "0" Then
        cell.EntireRow.Delete
    End If
Next

End Sub
 
Upvote 0
Unless I put it in here wrong, it isn't deleting the rows....See code below

Code:
Application.ScreenUpdating = False

Dim ChkRange As Range
Dim cell As Range
Dim Fullnme
Dim Filenme

Range("A6:G300").Select
Selection.clearcontents
Calculate


Sheetnme = Range("E2").Value


Filenme = Range("A2").Value
Fullnme = Range("A1").Value
       Workbooks.Open Filename:= _
        "H:\" & Fullnme
  Sheets("BSVariance").Select
  Range("A11:A145").Select
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("A6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  Sheets("BSVariance").Select
  Range("B11:B145").Select
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("B6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  Sheets("BSVariance").Select
  Range("G11:G145").Select
  Application.CutCopyMode = False
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("C6").Select
      Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  Range("L11:L145").Select
  Application.CutCopyMode = False
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("D6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Range("A5").Select
  Windows(Filenme).Activate
  Sheets("PLVariance").Select
  Range("A11:A94").Select
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("a141").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  Sheets("PLVariance").Select
  Range("B11:B94").Select
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("B141").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  Sheets("PLVariance").Select
  Range("G11:G94").Select
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("C141").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  Sheets("PLVariance").Select
  Range("L11:L94").Select
  Selection.Copy
  Windows("VarianceSummary.xls").Activate
  Range("D141").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  Windows(Filenme).Activate
  ActiveWindow.Close
  Windows("VarianceSummary.xls").Activate
  Range("a5").Select
  
  Set ChkRange = Range("C6:C224")
  For Each cell In ChkRange
    If cell = "0" Then
        cell.EntireRow.Delete
    End If
Next

Range("a5").Select
Application.ScreenUpdating = True

    
End Sub
 
Upvote 0
This seems to be the exact question that I need answered. But I don't understand the "code" used in the solution. Did you get it figured out? Is there a formula or condition? Is that answer found in the solution that is listed in the thread and maybe I just can't see it?

Please help if you can - thanks!

Jennifer
 
Upvote 0
Just a thought. Are you sure that all the values are in fact 0, and not text that looks like number?
 
Upvote 0
shades said:
Just a thought. Are you sure that all the values are in fact 0, and not text that looks like number?

I've actually changed it to look for the text "(none)", but it also acts the exact same when I have it looking for the number 0.
 
Upvote 0
I think I know what's wrong....

If, for example, it finds 0 at cell A1, it'll delete the row and then move to cell A2. It doesn't recognise that the value that was previously in A2 is now in A1, if you get what I mean.

How can this be worked around?
 
Upvote 0
Do the Loop in reverse order - i.e., start at the bottom. Something like the following (it will need to be fine tuned, but you should get the idea)

For each Rows.Count To 1 Step -1
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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