Excel 2021 - Catching the variable from the Drop Down Filter

BrerRabbit

Board Regular
Joined
Aug 20, 2023
Messages
66
Office Version
  1. 2021
  2. 2016
  3. 2013
Platform
  1. Windows
I've google this to no avail. The drop down filter, when you enter search criteria, is there anyway to catch this variable? I'm not looking to do a filter function with a separate table.

1701137248906.png
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
What do you mean by "catch this variable"? Catch it where exactly, and what do you intend to do with it after you've "caught" it?
 
Upvote 0
What do you mean by "catch this variable"? Catch it where exactly, and what do you intend to do with it after you've "caught" it?
When a search criteria is entered into the search box in the dropdown list, i'd like to see if that search criteria can be copied into a cell as well.

I don't like my chances, but I am wondering if it has been done.
 
Upvote 0
See if this does what you want. A number of steps to go through:
1. Place =CountA(A:A) on your sheet to enable the triggering of a Worksheet_Calculate event (you can hide it)
2. Put the following code in the sheet code area of the sheet you have the autofilter in (it's checking column B (2) change to suit, and putting the filter criteria into cell F1 - also change as needed)
VBA Code:
Private Sub Worksheet_Calculate()
    With ActiveSheet.AutoFilter.Filters(2)                              '<-- checking column 2 - change to suit
        If .On Then
            Range("F1").Value = Right(.Criteria1, Len(.Criteria1) - 1)  '<-- copied to cell F1 - change to suit
        Else
            Range("F1").Value = ""
        End If
    End With
End Sub
With the following starting sheet:
auto criteria (version 2).xlsb
ABCDEFG
1hdr1hdr2hdr3Search Value:7
2xax
3xax
4xbx
5xbx
6xaax
7xbbx
Sheet1
Cell Formulas
RangeFormula
G1G1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$7G1


Entering "a" as the autofilter criteria results in this:
auto criteria (version 2).xlsb
ABCDEFG
1hdr1hdr2hdr3Search Value:a7
2xax
3xax
8
Sheet1
Cell Formulas
RangeFormula
G1G1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$7G1


or "bb" results in this:
auto criteria (version 2).xlsb
ABCDEFG
1hdr1hdr2hdr3Search Value:bb7
7xbbx
8
Sheet1
Cell Formulas
RangeFormula
G1G1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$7G1


Clearing the filter does this:
auto criteria (version 2).xlsb
ABCDEFG
1hdr1hdr2hdr3Search Value:7
2xax
3xax
4xbx
5xbx
6xaax
7xbbx
8
Sheet1
Cell Formulas
RangeFormula
G1G1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$7G1
 
Upvote 0
A couple of questions, if that's ok.

1. I'm assuming that any counta or countif or count will trigger the VB code. Is that correct?
2. I get this error message when trying to run the code.
RUN TIME ERROR 91
OBJECT VARIABLE OR WITH BLOCK VARIABLE NOT SET

This is very cool btw.
 
Upvote 0
I usually just use a CountA() but feel free to experiment.
What line are you getting the Run Time Error on?
 
Upvote 0
I usually just use a CountA() but feel free to experiment.
What line are you getting the Run Time Error on?
This one: With ActiveSheet.AutoFilter.Filters(2)

It doesn't proceed beyond that point.
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,112
Members
449,096
Latest member
provoking

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