?
Solved

Finding error condition in a table

Posted on 2013-11-19
5
Medium Priority
?
149 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 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
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.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

749 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