Ask a question »

VBA - EXCEL - AutoFilter and Object Range Method

June 2015

One may need several types of filters in an Excel sheet to help with VBA programming. An Excel sheet is used to simplify tedious calculations and setting filters are important in the process. This will help in setting a range in Excel by using simple programs in VBA. The main query people have is how to retrieve objects in an object range format. The article below is a small VBA programming lesson on selecting an auto filter and object range in the field of a particular cell in an Excel sheet. Initiate it by starting a macro and end the sub VBA program. The cells must be active and not hidden by filters.

[VBA-EXCEL] - AutoFilter and Object Range Method




Issue


With VBA programming, it is possible to set filters on a sheet:
  • Selection.AutoFilter Field:=1, Criteria1:="FOO"
  • The filter displays a certain number of lines based on defined criteria.
  • How to retrieve these rows in a Range objects?

Solution


This should answer your problem:

Sub Macro2 ()      
    Sun rngSelect As Range      
          
    'I assumed that the filters are on line 1      
    Range ( "A1"). Select      
          
    'Filtering      
    'Selection.AutoFilter Field: = 1, Criteria1: = "FOO"      
    Selection.AutoFilter Field: = 7 Criteria1: = "* paris *"      
    'RngSelect = all cells visible (not hidden by the filter)      
    'Contiguous to the active cell      
    Set rngSelect = ActiveCell.CurrentRegion.SpecialCells (xlCellTypeVisible)      
          
    'Copy the selected area      
    rngSelect.Copy      
          
    Debug.Print rngSelect.Address      
    '...      
    'Commands      
    'Many and varied      
    '...      
          
    Set rngSelect = Nothing      
End Sub
For unlimited offline reading, you can download this article for free in PDF format:
Vba-excel-autofilter-and-object-range-method.pdf

See also

In the same category

Published by aakai1056. - Latest update by Virginia Parsons
This document entitled « VBA - EXCEL - AutoFilter and Object Range Method » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.