Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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

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 20161123
    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?



Recommended Productivity Tools for Excel

Kutools for Excel Helps You Always Finish Work Ahead of Time, and Stand Out From Crowd

  • More than 300 powerful advanced features, designed for 1500 work scenarios, increasing productivity by 70%, give you more time to take care of family and enjoy life.
  • No longer need memorizing formulas and VBA codes, give your brain a rest from now on.
  • Become an Excel expert in 3 minutes, Complicated and repeated operations can be done in seconds, 
  • Reduce thousands of keyboard & mouse operations every day, say goodbye to occupational diseases now.
  • 110,000 highly effective people and 300+ world-renowned companies' choice.
  • 60-day full features free trial. 60-day money back guarantees. 2 years of free upgrade and support.

Brings Tabbed Browsing and Editing to Microsoft Office, Far More Powerful Than The Browser's Tabs

  • Office Tab is designed for Word, Excel, PowerPoint and Other Office Applications: 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!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Matt Foulkes-Needham · 2 months ago
    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
  • To post as a guest, your comment is unpublished.
    Daniel · 5 months ago
    Buenas tardes

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

    Saludos
  • To post as a guest, your comment is unpublished.
    Heitor · 1 years ago
    Muito bom!
    Entretanto, só funciona em um intervalo.
    Eu queria aplicá-lo em uma Tabela, é possível? Ficaria muito agradecido se sim!!