Solved

Finding error condition in a table

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

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