?
Solved

Issue with deleting cells

Posted on 2011-10-14
3
Medium Priority
?
151 Views
Last Modified: 2012-06-27
I've got code that is supposed to delete any rows that contain null values or a 0 in rows B and D in one worksheet and Row C in another worksheet. It is clearing out most of the rows, but I still have rows that have Zeros in them being left behind. I've attached a copy of the workbook I'm working on.

The sub routine in question is CleanUP()

You can see the results by looking at Labor Totals  

I need those rows with Zeros to be gone but I cant figure out why its failing to remove them.


Sub CleanUP()

Dim rData As Range

Application.ScreenUpdating = False

With Sheets("Labor Totals")
    .Range("A1").Value = "a"
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=2, Criteria1:="0", Operator:=xlOr, Criteria2:=""
    .Range("A1").AutoFilter Field:=4, Criteria1:="0", Operator:=xlOr, Criteria2:=""
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
            rData.EntireRow.Delete
        End If
    End With
    .AutoFilterMode = False
    .Range("A1").Clear
End With

Set rData = Nothing

With Sheets("EQ Totals")
    .Range("A1").Value = "a"
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=3, Criteria1:="0", Operator:=xlOr, Criteria2:=""
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
            rData.EntireRow.Delete
        End If
    End With
    .AutoFilterMode = False
    .Range("A1").Clear
End With

Application.ScreenUpdating = True

End Sub  
Copy-of-working.xlsm
0
Comment
Question by:RedstoneIT
  • 2
3 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 36971833
You are filtering with an AND criteria on columns B and D in Labor Rates. Try commenting out the following line of code:
    .Range("A1").AutoFilter Field:=4, Criteria1:="0", Operator:=xlOr, Criteria2:=""
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 36971840
If you mean to have an OR criteria on columns B and D, then try doing filters on columns B and D separately:

Sub CleanUP()

Dim rData As Range

Application.ScreenUpdating = False

With Sheets("Labor Totals")
    .Range("A1").Value = "a"
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=2, Criteria1:="0", Operator:=xlOr, Criteria2:=""
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
            rData.EntireRow.Delete
        End If
    End With
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=4, Criteria1:="0", Operator:=xlOr, Criteria2:=""
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
            rData.EntireRow.Delete
        End If
    End With
    .AutoFilterMode = False
    .Range("A1").Clear
End With

Set rData = Nothing

With Sheets("EQ Totals")
    .Range("A1").Value = "a"
    .AutoFilterMode = False
    .Range("A1").AutoFilter Field:=3, Criteria1:="0", Operator:=xlOr, Criteria2:=""
    With .AutoFilter.Range
        On Error Resume Next
        Set rData = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
        If Not rData Is Nothing Then
            rData.EntireRow.Delete
        End If
    End With
    .AutoFilterMode = False
    .Range("A1").Clear
End With

Application.ScreenUpdating = True

End Sub

Open in new window

0
 

Author Comment

by:RedstoneIT
ID: 36971844
Any idea why it wont work on field 8 ? Column G ?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question