Real-time filtering in Excel

Steve_Brady used Ask the Experts™

This is a follow-up question to an already-resolved thread located here:

In that thread, a VBA solution was provided which enables a single row of cells, located just above the column headings row of a table of data, to be used for two-step filtering. In other words, without the need to open any filtering boxes from the ribbon or quick access toolbar, a filtering criterion can be entered directly into a cell (1st step) and then by simply pressing {Enter} (2nd step), the filtering is done.

With that code now available, the purpose of this thread is to inquire about the feasibility of turning the above-mentioned two-step filtering process into a one-step process. In other words, is it possible to eliminate the 2nd step mentioned above (pressing {Enter}) so that real-time filtering occurs as you are entering a filtering criterion? This would be identical to the dynamic process which takes place when a drop-down auto-filtering box is open and the list of possible matches automatically adjusts in response to each new character entered into the search box.

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi Steve,

Unfortunately, there is no worksheet event to handle a keystroke in a cell.  The code from the previous question uses the worksheet change event which fires after a cell is committed (i.e. when enter is pressed).  For something like that you would have to use another solution such as a VB/C# project using a datagridview that can handle many more events.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

Add this to your code which will send 'Enter'

SendKeys "~"
Unless I am not understanding correctly, he wants to filter in real time, so when "A" is pressed, it will filter A's, then a "D" is typed, it will filter by AD...

Basically like a SuggestAppend auto complete mode in a combobox as he mentioned.

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!


I read the question like you did FM.

I suppose text-box controls could be placed (exactly) over the filter criteria cells, with a single line as a border, a transparent back style, & a Z-Order "at the front" so that they look like worksheet cells.

Such a control will, of course, provide KeyDown, KeyUp, &/or KeyPress event handlers that code can be added within to support the functionality requested.


Older than dirt
Most Valuable Expert 2017
Distinguished Expert 2018
I suppose text-box controls could be placed (exactly) over the filter criteria cells...
@Steve_Brady: The attached workbook illustrates an improvement on fanpages suggestion. It floats a single textbox over any cell in a range (in this case A2:F2). On exiting the cell the cell's text is set to the textbox's text, and while entering text in the textbox you can trigger your filtering from its Change event.

Here's the complete code:
Module1 Code

The grngCurrent variable is used to store the range of the cell your are in. If you run theShowForTesting macro you can then go into Design Mode and change whatever properties of the textbox that you like.
Option Explicit
Public grngCurrent As Range

Public Sub ShowForTesting()
  With ActiveSheet.txtFloat
    .Top = 10
    .Left = 10
    .Width = 50
    .Height = 50
    .Visible = True
    .Text = "This is txtFloat"
  End With

End Sub

Open in new window

Sheet Code
Option Explicit
' Highly modified code that was originally Developed by Contextures Inc.

Private Sub txtFloat_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
    Set grngCurrent = ActiveCell
    ' Move to next cell on Enter and Tab
    Select Case KeyCode
        Case 9
            ActiveCell.Offset(0, 1).Activate
        Case 13
            ActiveCell.Offset(1, 0).Activate
    End Select

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim ws As Worksheet
Set ws = ActiveSheet

' Set the range where you want the textbox to appear
If Intersect(ActiveCell, Range("A2:F2")) Is Nothing Then
    txtFloat.Visible = False
    Exit Sub
End If

On Error Resume Next

If Not grngCurrent Is Nothing Then
    grngCurrent.Value = txtFloat.Text
End If
Set grngCurrent = ActiveCell

Application.EnableEvents = False
Application.ScreenUpdating = False

If Application.CutCopyMode Then
  'allows copying and pasting on the worksheet
  GoTo errHandler
End If

With txtFloat
    .ListFillRange = ""
    .LinkedCell = ""
    .SpecialEffect = fmSpecialEffectFlat
    .Visible = True
    .Left = Target.Left
    .Top = Target.Top
    .Width = Target.Width
    .Text = Target.Value
End With

  Application.ScreenUpdating = True
  Application.EnableEvents = True

End Sub

Open in new window



Many thanks.
Martin LissOlder than dirt
Most Valuable Expert 2017
Distinguished Expert 2018

You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial