Consulting

Results 1 to 3 of 3

Thread: Solved: when autofilter returns nothing

  1. #1
    VBAX Tutor
    Joined
    Nov 2007
    Posts
    291
    Location

    Solved: when autofilter returns nothing

    Hi,

    I am using autofilter in code to get a large list of data to only give me certain information which I copy and paste in different sheets.

    However some days one or more of the autofilters may return zero results (depending on the data download)

    Is there a way to capture is autofilter returns nothing as I set the range as cell A2 to the lastrow (which I have calculated at the beginning of the code) using xlvisiblecells and if it returns nothing it thinks the rest of the blank sheet is the range.

    Is there any way to say if the results are zero then do nothing

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Maybe something along these lines:
    [VBA]
    Sub Macro1()
    Dim rFound As Variant
    On Error GoTo Nxt
    rFound = Sheets("Sheet1").UsedRange.Find(What:="", After:=Range("A1"), LookIn:=xlValues, LookAt:=xlPart).Value
    Nxt:
    If rFound = vbNullString Or rFound = 0 Then
    MsgBox "Doing nothing!"
    Else
    MsgBox "found it "
    End If
    End Sub
    [/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Regular
    Joined
    Sep 2007
    Location
    Singapore
    Posts
    63
    I work with Autofilters a lot too. Here's what I use:

    [vba] With .AutoFilter.Range
    Counter = .Columns(1).SpecialCells(xlCellTypeVisible).Count
    If Counter = 1 Then
    'Occurs when no records are returned. Exit Sub here or something
    End If
    End With[/vba]
    Basically, I count the number of visible rows. When it's 1, it means only the header is there and no records are returned.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •