Solved

Finding error condition in a table

Posted on 2013-11-19
5
142 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
  • 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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
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…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now