Excel UDF to values based on Filtered or Unfiltered Values in an Autofilter.

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,

I found an Excel UDF that shows items filtered or checked in Autofilter. I wanted both show items unfiltered or unchecked in Autofilter based boolean statement passed optional statement.

Does anyone know how I can pass a Boolean statement to get Filtered or Unfiltered Items in Autofilter?


VBA Code:
Public Function ShowFilter(Rng As Range)
Dim filt As Filter
Dim sCrit1 As String
Dim sCrit2 As String
Dim sOp As String
Dim lngOp As Long
Dim lngOff As Long
Dim frng As Range
Dim sh As Worksheet
Dim i As Long
 

    Set sh = Rng.Parent
    If sh.FilterMode = False Then
        ShowFilter = "No Active Filter"
        Exit Function
    End If
    Set frng = sh.AutoFilter.Range
 
    If Intersect(Rng.EntireColumn, frng) Is Nothing Then
        ShowFilter = CVErr(xlErrRef)
    Else
        lngOff = Rng.Column - frng.Columns(1).Column + 1
        If Not sh.AutoFilter.Filters(lngOff).On Then
            ShowFilter = "No Conditions"
        Else
            Set filt = sh.AutoFilter.Filters(lngOff)
            On Error Resume Next
            lngOp = filt.Operator
            If lngOp = xlFilterValues Then
           
                For i = LBound(filt.Criteria1) To UBound(filt.Criteria1)
                    sCrit1 = sCrit1 & filt.Criteria1(i) & " or "
                Next i
                sCrit1 = Left(sCrit1, Len(sCrit1) - 3)
            Else
           
                sCrit1 = filt.Criteria1
                sCrit2 = filt.Criteria2
                If lngOp = xlAnd Then
                    sOp = " And "
                ElseIf lngOp = xlOr Then
                    sOp = " or "
                Else
                    sOp = ""
                End If
            End If
            ShowFilter = sCrit1 & sOp & sCrit2
        End If
    End If
End Function

Your help would be greatly appreciated.


Kind Regards


Biz
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,215,002
Messages
6,122,652
Members
449,092
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top