VBA code. Filter data in table by clicking a cell.

Americomski

New Member
Joined
Apr 4, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a sample problem. Lets say my table range is A2:A50. In this column, each row has either A, B, C or D

Now lets treat cell B2 as the "A" button. I need a formula so that when I click cell B2, the table range A2:50 is filtered to only include rows that have the letter "A" in them.

Thank you.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi and welcome to MrExcel!
The next option is with VBA code. I suggest you click on cell B1 instead of B2.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  If Target.CountLarge > 1 Then Exit Sub
  If Target.Address = "$B$1" Then
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    Range("A:A").AutoFilter 1, Target.Value
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0

Forum statistics

Threads
1,215,419
Messages
6,124,798
Members
449,189
Latest member
kristinh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top