Forum Discussion
Custom Number Filtering
- Dec 01, 2023
leisaellemor To automate this with VBA, open the Visual Basic editor (Alt+F11). In the left-hand pane under VBAProject > Microsoft Excel Objects, double-click the sheet name that contains your data to open the worksheet module. The code will vary slightly, depending on if your data has been formatted as an Excel Table, or if it's just a regular worksheet range.
Version 1: With a Structured Excel Table
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) 'Stop macro if more than once cell was changed If Target.Cells.Count > 1 Then Exit Sub 'Apply filter if the criteria cell was changed Dim rg As Range Set rg = Me.Range("C1") If Not Intersect(Target, rg) Is Nothing Then Me.ListObjects(1).Range.AutoFilter Field:=3, criteria1:="<=" & rg.Value End If End Sub
Version 2: With a Worksheet Range
Option Explicit Private Sub Worksheet_Change(ByVal Target As Range) 'Stop macro if more than once cell was changed If Target.Cells.Count > 1 Then Exit Sub 'Apply filter if the criteria cell was changed Dim rg As Range Set rg = Me.Range("C1") If Not Intersect(Target, rg) Is Nothing Then Dim rgData As Range Set rgData = Me.Range("A2").CurrentRegion Set rgData = rgData.Offset(1).Resize(rgData.Rows.Count - 1) rgData.AutoFilter Field:=3, Criteria1:="<=" & rg.Value End If End Sub
Note: since the screenshot of your data does not reveal the worksheet column letters or rows numbers, I made the assumption that the table begins in cell A2, the column to filter is Field:=3 and the criteria to filter by is in cell C1. Please adjust the range references and field number, if necessary.
Also, Range("A2").CurrentRegion was used here to identify the entire data range. This method will only work properly if your data range does not contain any completely blank rows or columns... the CurrentRegion method identifies all adjacent data in the same manner that pressing Ctrl+Shift+8 (*) will highlight all adjacent data to the active cell. If your data contains additional adjacent cells above the header row, you may need to adjust the Offset and Resize numbers accordingly.
Then, just input a new value in the criteria cell to refresh the filter.
mathetes Thankyou. That is part of the problem solved.
However, I don't want the data to be filtered by duplicating into other cells. I am wanting the data to be filtered where it sits originally so that all of the other column data on the same line is still visible. This is the reason I was trying to use the inbuilt filtering as this allows me to keep all of the other column data on the same line.
Any idea if this is possible?
Thankyou. That is part of the problem solved.
However, I don't want the data to be filtered by duplicating into other cells. I am wanting the data to be filtered where it sits originally so that all of the other column data on the same line is still visible. This is the reason I was trying to use the inbuilt filtering as this allows me to keep all of the other column data on the same line.
Any idea if this is possible?
A design principle for you, if I might be so bold: separate input or raw data from output.
Or, in other words, don't try to do everything on the same single sheet. Doing this--separating input/raw data from output--would enable you to display or report based on your data in more creative ways. Excel is excellent at this kind of thing, extracting data from a large dataset and producing useful summaries, high level analyses etc. It's a mistake to mix the input and output in the same space. And can lead to unintentional errors. [There have been many people posting their alarm here at having data disappear, only to realize that they'd used that tool bar filter and forgotten having done so.]
The FILTER function enables you to produce a good output page, in the process doing what you want, filtering (to quote) "so that all of the other column data on the same line is still visible, and it can do that cleanly on an output tab of its own. If you incorporate the SORT function as well, it will still keep all the data on any given line together yet it can also sort for other output (reporting) purposes. That too is explained on the YouTube video my first post linked to.