PC Review


Reply
Thread Tools Rate Thread

Autofilter and cell check

 
 
JLR-Mart
Guest
Posts: n/a
 
      12th May 2009
I have a macro where I want to set autofilter to filter for certain criteria
and the I want to check the value of a certain column for each row that is
visible after filtering. The worksheet may contain several thousand rows and
I don't want to check each row for a match as it takes too long. I want to
filter and then just check the rows that remain visible.

Any ideas?
 
Reply With Quote
 
 
 
 
OssieMac
Guest
Posts: n/a
 
      12th May 2009
The following is a full example of how to work with AutoFiltered range. It
includes comments on what the code is doing plus some commented out code that
you probably won't need but is there for information for selecting all the
visible data. I have left uncommented the line of code to select an
individual column.

The following you may be aware of but just in case it saves you having to
get back to me if you do not know about it.

Note: When using Offset(row,column) the value of offset is the number of
times you would have to press an arrow key to arrive at the row or column. It
is not a count of the columns or column number.

Also note that a space and underscore at the end of a line is a line break
in anotherwise single line of code.

Sub SelectAutoFilteredData()
Dim rngVisible As Range
Dim c As Range

'Should always test for AutofilterMode and Filter Mode _
to ensure working with a filtered range to avoid code errors.
If Sheets("Sheet1").AutoFilterMode Then 'Test if filter arrows present
If Sheets("Sheet1").FilterMode Then 'Test if actually filtered

With Sheets("Sheet1").AutoFilter.Range

'Next line returns number of visible cells divided _
by number of columns in autofilter range. _
If greater than 1 then some data is visible. _
Equal to 1 then only column headers visible
If .SpecialCells(xlCellTypeVisible).Count / _
.Columns.Count > 1 Then

'Select all visible data. Offset to row below column _
headers and resize to one row less to account for _
not including column headers.
'Set rngVisible = .Offset(1, 0) _
.Resize(.Rows.Count - 1, .Columns.Count) _
.SpecialCells(xlCellTypeVisible)

'Alternative to previous line of code. _
Defaults to number of columns in AutoFilter.Range _
without specifying first or last Column numbers.
'Set rngVisible = .Offset(1) _
.Resize(.Rows.Count - 1) _
.SpecialCells(xlCellTypeVisible)

'To select only one column, set the Offset _
1 row down and X columns across and then _
Resize to only 1 column wide.
Set rngVisible = .Offset(1, 3) _
.Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
End If

End With

Else
'used for testing
MsgBox "No filters have actually been set"
End If

Else
'Used for testing
MsgBox "AutoFilter mode has not been set on the worksheet"

End If


For Each c In rngVisible
MsgBox c.Address 'for testing only
'Insert your code here in lieu of msgbox
'Something like the following:-
'If c.value = "Whatever" then
'your code etc
'End If

Next c

End Sub

--
Regards,

OssieMac


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check to see if Autofilter is Engaged asmithbcat Microsoft Excel Misc 7 25th Apr 2008 04:54 PM
Autofilter Check Mark Ivey Microsoft Excel Programming 5 26th Jan 2008 10:56 PM
check boxes + autofilter - is it possible? Dirk Diggler Microsoft Excel Discussion 5 22nd Mar 2007 12:49 AM
check boxes + autofilter - is it possible? Dirk Diggler Microsoft Excel Programming 1 21st Mar 2007 08:51 PM
Cannot check autofilter - Grey'd out Microsoft Excel Worksheet Functions 2 8th Jan 2004 04:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:15 AM.