Solved

Finding error condition in a table

Posted on 2013-11-19
5
146 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 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

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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.

690 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