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:
Best Office Productivity Tools
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...
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!