Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default Autofilter and cell check

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Autofilter and cell check

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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check to see if Autofilter is Engaged asmithbcat Excel Discussion (Misc queries) 7 April 25th 08 04:54 PM
Autofilter Check Mark Ivey Excel Programming 5 January 26th 08 10:56 PM
Check if Conditional Format is True or False / Check cell Color Kevin McCartney Excel Worksheet Functions 5 June 29th 07 11:12 AM
check boxes + autofilter - is it possible? Dirk Diggler Excel Programming 1 March 21st 07 08:51 PM
Create and Link check box with autofilter and sum, average of filtered raws. Madiya Excel Programming 1 January 12th 04 09:05 AM


All times are GMT +1. The time now is 05:12 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"