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.
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:
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
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:
More related articles:
You are guest
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 2 years agoHi, 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.· 2 years agoBuenas tardes
Excelente aportación ... lástima que no funcione con un archivo protegido ... qué podría cambiarle para que si funcione?
To post as a guest, your comment is unpublished.· 2 years agoMuito bom!
Entretanto, só funciona em um intervalo.
Eu queria aplicá-lo em uma Tabela, é possível? Ficaria muito agradecido se sim!!