View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Test if Column B is autofiltered

Maybe I misudnerstood the objective, as written returns true/false if a
filter exists in the column (not necessarily selected). Is this is what you
want

Sub test()

MsgBox ColFilterOn(ActiveSheet, "B")
MsgBox ColFilterOn(ActiveSheet, 2)

End Sub

Function ColFilterOn(ws As Worksheet, col) As Boolean
Dim af As AutoFilter, f As Filter

If VarType(col) = vbString Then
col = ws.Range(col & 1).Column
End If

On Error GoTo errExit
Set af = ws.AutoFilter
On Error GoTo 0
If Not af Is Nothing Then
If Not Intersect(Columns(col), af.Range) Is Nothing Then
For Each f In af.Filters
If f.Parent.Range.Column = col Then
ColFilterOn = f.On
Exit For
End If
Next
End If
End If

errExit:

End Function

Regards,
Peter T

"CLR" wrote in message
...
Thanks Peter, but this one returns "true" no matter what column is
filtered
on....or actually if none are and just the Autofilter is turned on.

Vaya con Dios
Chuck, CABGx3



"Peter T" wrote:

Another way (anywhere in col-b)

Sub test()

MsgBox ColFilterOn(ActiveSheet, "B")
MsgBox ColFilterOn(ActiveSheet, 2)

End Sub

Function ColFilterOn(ws As Worksheet, col) As Boolean
Dim af As AutoFilter

If VarType(col) = vbString Then
col = ws.Range(col & 1).Column
End If

On Error GoTo errExit
Set af = ws.AutoFilter
On Error GoTo 0
If Not af Is Nothing Then
ColFilterOn = Not Intersect(Columns(col), af.Range) Is Nothing
End If

errExit:

End Function

Regards,
Peter T

"CLR" wrote in message
...
Thanks Michael, but I guess I wasn't clear in my post. I only want my
macro
to run IF the Autofilter is on for Column B, AND a selection has been
made
in
Column B. Column B is a list of names, and I only want to run the
macro
against ONE SELECTED name.

How can I modify your code to do that?

Vaya Con Dios,
Chuck, CABGx3



"Michael" wrote:

You can modify this instruction to be run on one or multiple columns;
as
is,
it will check in the whole sheet.
If Worksheets("Sheet1").AutoFilterMode Then
isOn = "On"
Else
isOn = "Off"
End If
MsgBox "AutoFilterMode is " & isOn

--
If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.




"CLR" wrote:

Hi All.....
I have a macro that I would like to first check if the sheet1 has
been
Autofiltered for any selection in Column B before it will continue
to
run.......the sheet may or may not be also Autofiltered for other
columns,
but it must also be Autofiltered for a selection in column B to
qualify.

Any help would be much appreciated.

TIA
Vaya con Dios,
Chuck, CABGx3