Skip to main content

How to filter data just by clicking cell content in Excel?

Author: Xiaoyang Last Modified: 2020-07-24

Normally, in Excel, you can apply the Filter function to filter data by your need. But, in this article, I will talk about how to filter data just by clicking a cell value. Take the following data as example, when I double click the cell A2, all corresponding records based on this clicked value will be filtered at once as below screenshot shown.

doc filter by click 1

Filter data just by clicking a cell value with VBA code


arrow blue right bubble Filter data just by clicking a cell value with VBA code

Here, I will provide a VBA code for you to filter data by clicking a cell, please do as follows:

1. First, give your data a range name, please enter a range name into the Name Box, and press Enter key, see screenshot:

doc filter by click 2

2. Right click the sheet tab which you want to filter by clicking cell, and then choose View Code from the context menu, and in the opened Microsoft Visual Basic for Applications window, copy and paste the following code into the blank Module:

VBA code: Filter data by clicking cell value:

Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
    Dim rgTable As Range
    Dim rgData As Range
    Dim xColumn As Integer
    On Error Resume Next
    Application.ScreenUpdating = False
    Set rgTable = Range("mydata")
    With rgTable
        Set rgData = .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
        If Not Application.Intersect(ActiveCell, rgData.Cells) Is Nothing Then
            xColumn = ActiveCell.Column - .Column + 1
            If ActiveSheet.AutoFilterMode = False Then
                .AutoFilter
            End If
            If ActiveSheet.AutoFilter.Filters(xColumn).On = True Then
                .AutoFilter Field:=xColumn
            Else
                .AutoFilter Field:=xColumn, Criteria1:=ActiveCell.Value
            End If
        End If
    End With
    Set rgData = Nothing
    Set rgTable = Nothing
    Application.ScreenUpdating = True
End Sub

doc filter by click 3

Note: In the above code, mydata is the range name you have created for your data range, please change it to your need.

3. Then save and close the code window, now, when you double click any cell of the data range that you want to filter out, its corresponding records will be filtered as following screenshot shown:

doc filter by click 4


More related articles:

How to change cell value by clicking on the cell?

How to sort column data by clicking on header in Excel?

Best Office Productivity Tools

ūü§Ė Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions‚Ķ
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (3)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, I have a summary sheet, that calculates how many lines on the main sheet that are within a certain date range. I want to be able to double click on a certain date range and it then filter out the data on the main sheet to the date range I double clicked on. What code would I need for this please
This comment was minimized by the moderator on the site
Buenas tardes

Excelente aportación ... lástima que no funcione con un archivo protegido ... qué podría cambiarle para que si funcione?

Saludos
This comment was minimized by the moderator on the site
Muito bom!
Entretanto, só funciona em um intervalo.
Eu queria aplicá-lo em uma Tabela, é possível? Ficaria muito agradecido se sim!!
There are no comments posted here yet
Leave your comments
Posting as Guest
Rate this post:
0   Characters
Suggested Locations