?
Solved

Finding error condition in a table

Posted on 2013-11-19
5
Medium Priority
?
147 Views
Last Modified: 2013-11-19
I have an autofilter applied to a table. The table headers are

Ename      Ecode      Division      Date      Project      Hours      Ccode

I would like a code which would search under all headers and filter out all rows which have either an error condition like NA, REF, VALUE or have a blank cell. The objective is to get rid of all such conditions so it would be just fine if it filters out only the first set of such items. Once corrected, it can look for the next set.

I am uploading a file for ease of testing.
Filter-sample.xlsx
0
Comment
Question by:Saqib Husain, Syed
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:Michael
ID: 39659095
Maybe something like this to loop through all columns and filter for errors:
Sub filterForError()
    Dim i As Integer
    Dim rng As Range
    
    Set rng = Range("A1").CurrentRegion
    For i = 1 To rng.Columns.Count
        rng.AutoFilter Field:=i, Criteria1:=Array("#REF!", "#VALUE!", "#N/A", "="), Operator:=xlFilterValues
        'Do something with the filtered result
        rng.AutoFilter Field:=i
    Next i
End Sub

Open in new window


Joop
0
 
LVL 43

Author Comment

by:Saqib Husain, Syed
ID: 39659252
For the "Do something ..."
I would simply like to have an exit sub but only if it has found some filtered data otherwise continue.
0
 
LVL 6

Accepted Solution

by:
Michael earned 2000 total points
ID: 39659310
Ok, something like this may work:
Sub filterForError()
    Dim i As Integer
    Dim rng As Range, rngData As Range
    
    Set rng = Range("A1").CurrentRegion
    Set rngData = rng.Offset(1).Resize(rng.Rows.Count - 1)
    
    For i = 1 To rng.Columns.Count
        rng.AutoFilter Field:=i, Criteria1:=Array("#REF!", "#VALUE!", "#N/A", "="), Operator:=xlFilterValues
        
        On Error Resume Next
        If rngData.SpecialCells(xlCellTypeVisible).Rows.Count < 1 Then
            rng.AutoFilter Field:=i
        Else
            Exit Sub
        End If
    Next i
End Sub

Open in new window

Joop
0
 
LVL 43

Author Closing Comment

by:Saqib Husain, Syed
ID: 39659351
Perfect, thanks a lot.

I myself also managed to exit sub using this statement

If rng.SpecialCells(xlCellTypeVisible).Cells.Count > rng.Columns.Count Then Exit Sub
0
 
LVL 6

Expert Comment

by:Michael
ID: 39659372
That seems even better!
Thanks for sharing. I'll remember that one.

Regards,
Joop
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

777 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