Enter in Cell the Column Letter that is Filtered

Michael Fowler

New Member
Joined
Jan 23, 2024
Messages
28
Office Version
  1. 2021
  2. 2016
Platform
  1. Windows
Is there a way that you can automate into a cell (either by formula or macro) the Column Letter that is filtered ?

Example : Column D is filtered, so as a result I want "D" or the Column Heading Name automatically entered into a cell.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Is there a way that you can automate into a cell (either by formula or macro) the Column Letter that is filtered ?

Example : Column D is filtered, so as a result I want "D" or the Column Heading Name automatically entered into a cell.
try:
VBA Code:
Function DetectFilter() 'get filtered columns index
    Dim i As Long, j As Long, k As Long
    Dim flt
    Dim icoll As New Collection
    If Not ActiveSheet.FilterMode Then
        DetectFilter = "not filtered"
        Exit Function
    End If
    For i = 1 To ActiveSheet.AutoFilter.Filters.Count
        If ActiveSheet.AutoFilter.Filters(i).On Then icoll.Add i
    Next i
    If icoll.Count > 0 Then
        ReDim flt(1 To icoll.Count, 1 To 1)
        For j = 1 To icoll.Count
            flt(j, 1) = icoll(j)
        Next j
        DetectFilter = flt
    Else
        DetectFilter = "Nothing filtered"
    End If
End Function

in the cell in want to get result:
Excel Formula:
=IF(ISTEXT(DetectFilter()),"Not filtered",INDEX("your filter range",1,DetectFilter()))
 
Upvote 0
try:
VBA Code:
Function DetectFilter() 'get filtered columns index
    Dim i As Long, j As Long, k As Long
    Dim flt
    Dim icoll As New Collection
    If Not ActiveSheet.FilterMode Then
        DetectFilter = "not filtered"
        Exit Function
    End If
    For i = 1 To ActiveSheet.AutoFilter.Filters.Count
        If ActiveSheet.AutoFilter.Filters(i).On Then icoll.Add i
    Next i
    If icoll.Count > 0 Then
        ReDim flt(1 To icoll.Count, 1 To 1)
        For j = 1 To icoll.Count
            flt(j, 1) = icoll(j)
        Next j
        DetectFilter = flt
    Else
        DetectFilter = "Nothing filtered"
    End If
End Function

in the cell in want to get result:
Excel Formula:
=IF(ISTEXT(DetectFilter()),"Not filtered",INDEX("your filter range",1,DetectFilter()))
Thanks, but I need clarification ... Normally I trigger VBA code via a Macro, and after inserting your code above into the View Code via right-clicking the sheet tab, it doesn't appear in the list of Macros, so how do I trigger the Code ?
 
Upvote 0
It's a user-defined function, so you enter it as if it were an Excel function (like =SUM, =VLOOKUP etc) into a cell in your spreadsheet (=DetectFilter()).

@eiloken has provided you with the way to use it in a formula to get the result you're after.
in the cell in want to get result:
Excel Formula:
=IF(ISTEXT(DetectFilter()),"Not filtered",INDEX("your filter range",1,DetectFilter()))


after inserting your code above into the View Code via right-clicking the sheet tab
Note that the code needs to be inserted as a new module, not as event code in a worksheet.
1713936207167.png


Regards

Murray
 
Upvote 0
It's a user-defined function, so you enter it as if it were an Excel function (like =SUM, =VLOOKUP etc) into a cell in your spreadsheet (=DetectFilter()).

@eiloken has provided you with the way to use it in a formula to get the result you're after.




Note that the code needs to be inserted as a new module, not as event code in a worksheet.
View attachment 110409

Regards

Murray
Hi Murray. I've inserted the VBA Code as a new module, and I also have the Excel formula in a cell. They're sitting there, but how do I get it to run/activate/do its work ?
 
Upvote 0
Where the formula says "your filter range" you should enter the data filter range without the quotes, just like a normal excel range. It will just work.

For example:

1713937676206.png


Filter on column B

Book1
ABCD
1
2B
3
4ABCD
8261014
9261014
10261014
17
Sheet1
Cell Formulas
RangeFormula
A2A2=IF(ISTEXT(DetectFilter()),"Not filtered",INDEX(A4:D16,1,DetectFilter()))
B8:D10B8=A8+4
 
Upvote 0
Hi Murray. I've inserted the VBA Code as a new module, and I also have the Excel formula in a cell. They're sitting there, but how do I get it to run/activate/do its work ?
like myall_blues explain, just make sure enable macro and it will work like normal formula (just change "your filter range" in INDEX formula to the range that you filtered
 
Upvote 0
like myall_blues explain, just make sure enable macro and it will work like normal formula (just change "your filter range" in INDEX formula to the range that you filtered
Okay, I have made progress, but if I have more than one column filtered the formula result is only showing one of them, whereas I need it to show all filtered columns.
 
Last edited:
Upvote 0
Okay, I have made progress, but if I have more than one column filtered the formula result is only showing one of them, whereas I need it to show all filtered columns.
if worked for me, be sure you place formula at range that not in filter range and not has any data, formula will return an array if you have more than one column filtered:
 

Attachments

  • Screenshot (9).png
    Screenshot (9).png
    17.4 KB · Views: 2
Upvote 0
if worked for me, be sure you place formula at range that not in filter range and not has any data, formula will return an array if you have more than one column filtered:
I think I've got it right. My formula is ... =IF(ISTEXT(DetectFilter()),"Not Filtered",INDEX(B7:BF5007,1,DetectFilter()))

Looking at your screenshot, do you also have a formula in cell B2 ?
 
Upvote 0

Forum statistics

Threads
1,215,483
Messages
6,125,063
Members
449,206
Latest member
Healthydogs

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