Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > Filter / Range question


Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2010, 02:15 AM
valo valo is offline
Newcomer
 
Join Date: Jul 2008
Posts: 22
Default Filter / Range question


I have a table with few columns and lots of rows, which I filter by one field. This field can contain up to three kinds of values, say A, B and C, and after each filtering I need to copy the filtered data into another sheet and work on it there.

The problem comes when when there's no data corresponding to C value (there's no C value in the table). So the filtering process returns only the table header, which is fine, but I have no ideea of how to tell the compiler that there's no more data to select. The copy method selects all the cells in the sheet (the table header and ALL the blank cells) and then I paste them in the new sheet. When I do that, the scroll bar shrinks to its limits and the Ctrl+End shortcut, which initially was placing the cursor on the last cell of the table, now places it on the 'Cells(1048576, X)', and unfortunately this is not acceptable.

The question is how to avoid this situation? How can I figure out if the filtering returned rows or not? The Range("A1").End(xlDown).Row and Offset(x,y) commands are useless, since I navigate through filtered (basically hidden) cells.
Reply With Quote
  #2  
Old 07-22-2010, 03:08 AM
Colin Legg's Avatar
Colin Legg Colin Legg is offline
Out Of Office

Retired Moderator
* Expert *
 
Join Date: Mar 2005
Location: London, UK
Posts: 3,398
Default

Hi,

One way is to use Range.SpecialCells(xlCellTypeVisible).

Here's an example with a little defensive coding:
Code:
Sub Test()

    Dim rngFilter As Range
    Dim rngVisible As Range
    
    'is there an autofilter on sheet1?
    If Sheet1.AutoFilterMode Then
    
        'are any filters being used?
        If Sheet1.FilterMode Then
        
            'get a reference to the autofilter range
            'excluding the header row
            With Sheet1.AutoFilter.Range
                Set rngFilter = .Offset(1).Resize(.Rows.Count - 1, .Columns.Count)
            End With
        End If
    End If
    
    
    If rngFilter Is Nothing Then
        MsgBox "no filtering is being applied!"
        
    Else
        
        'find the visible cells, if there are none then an error will be raised
        On Error Resume Next
        Set rngVisible = rngFilter.SpecialCells(xlCellTypeVisible)
        On Error GoTo 0

        If rngVisible Is Nothing Then
            MsgBox "there are no visible cells!"
        Else
            MsgBox rngVisible.Address
        End If
        
    End If
    
End Sub
This was written for XL 2003. The object model changed slightly in XL 2007, so if you are using XL 2007 or later then you may have to tweak the code a little.
__________________
RAD Excel Blog
Reply With Quote
  #3  
Old 07-22-2010, 04:36 AM
valo valo is offline
Newcomer
 
Join Date: Jul 2008
Posts: 22
Default

That worked just fine, Colin, thank you!

Quote:
Originally Posted by Colin Legg View Post
One way is to use Range.SpecialCells(xlCellTypeVisible).
What other option would I have?
Reply With Quote
Reply

Tags
excel, filter, vba


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->