Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

How to get exactly column index and filter value of filtered columns in excel VBA.
(get index and value in event Sheet activate)
I tried but it don't exactly, Please help me

Thanks in advance
Posted
Updated 27-May-13 16:05pm
v3
Comments
Maciej Los 27-May-13 9:33am    
Do you want to catch user selection?
What is this for?
Please, provide more details (example data and expected output).
Which version of MS Excel?
Maciej Los 1-Jul-13 15:27pm    
Does you problem is solved?

1 solution

Solution for MS Excel 2003

Steps to do:
1) Add new module (do not change it name: Module1)
VB
Option Explicit

Public ExcApp As MyApp


2) Add below code to ThisWorkbook module:
VB
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set ExcApp = Nothing
End Sub

Private Sub Workbook_Open()
Set ExcApp = New MyApp
End Sub


3) Add new Class Module (change it name to: MyApp)
VB
Option Explicit

Public WithEvents oApp As Application


Private Sub Class_Initialize()
Set oApp = Application
End Sub

Private Sub Class_Terminate()
Set oApp = Nothing
End Sub

Private Sub oApp_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim rng As Range, wsh As Worksheet, af As AutoFilter, f As Filter, i As Integer
Dim sTmp As String

On Error Resume Next

Set rng = Target
Set wsh = rng.Parent
Set af = wsh.AutoFilter

sTmp = "Filter range: " & af.Range.Address & vbCr
For Each f In af.Filters
    i = i + 1
    If f.On Then
        sTmp = sTmp & "Filter on: " & af.Range.Cells(af.Range.Rows(1).Row, i).Address & vbCr
        sTmp = sTmp & "Criteria 1: " & f.Criteria1 & vbCr
        sTmp = sTmp & "Operator: " & f.Operator & vbCr
        sTmp = sTmp & "Criteria 2: " & f.Criteria2
    End If
Next

If sTmp<>"" Then MsgBox sTmp, vbInformation, "Filter information..."

Set f = Nothing
Set af = Nothing
Set wsh = Nothing
Set rng = Nothing

End Sub


4. Save workbook, close it and open it again with "Enable macros" option.

That's all!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900