Forum Discussion
Excel 365 ActiveX Textbox for "Search as you type" not working on second monitor
- Oct 20, 2021
It isn't clear to me what you see in the two different windows when using laptop with an external monitor. Is it two different worksheets? Two views of the same worksheet?
I tested by creating a second window to display Sheet1. I sized the windows so one of them displayed just TextBox 1, while the other showed just the Table. When the textbox was in Window 2, I couldn't select it. When the textbox was in WIndow 1, the code shown below worked as desired.
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) Dim xStr As String, xName As String Dim xRg As Range Application.ScreenUpdating = False With TextBox1 xName = "Materials" xStr = .Text Set xRg = .Parent.ListObjects(xName).Range If xStr <> "" Then xRg.AutoFilter Field:=2, Criteria1:="*" & xStr & "*", Operator:=xlFilterValues Else xRg.AutoFilter Field:=2, Operator:=xlFilterValues End If End With End Sub
I suggest setting a "stop" in your code by clicking in the light gray border to the left of the statements. If successful, you should see a large maroon dot in the border.
Once the macro is invoked by typing in your textbox, code execution will stop at the statement with the stop. You may then hover over variables to see their value, or execute one statement at a time by hitting F8. If you hit F5, code execution continues until the next stop. You can drag the yellow arrow up and down in the left border to execute different statements (a little known and very useful trick when debugging).
You may also display the Immediate window (using the View...Immediate window ribbon item) and execute statements you type there; be sure to click to make the cursor at the right end of the line before hitting Enter. You may display values in the Immediate window by prefixing them with a question mark, e.g. ?xStr
These techniques help you identify the statement that isn't working right, what the problem is (most often a variable without a value or with an error value), how to remedy it, and even verify the fix.
It isn't clear to me what you see in the two different windows when using laptop with an external monitor. Is it two different worksheets? Two views of the same worksheet?
I tested by creating a second window to display Sheet1. I sized the windows so one of them displayed just TextBox 1, while the other showed just the Table. When the textbox was in Window 2, I couldn't select it. When the textbox was in WIndow 1, the code shown below worked as desired.
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim xStr As String, xName As String
Dim xRg As Range
Application.ScreenUpdating = False
With TextBox1
xName = "Materials"
xStr = .Text
Set xRg = .Parent.ListObjects(xName).Range
If xStr <> "" Then
xRg.AutoFilter Field:=2, Criteria1:="*" & xStr & "*", Operator:=xlFilterValues
Else
xRg.AutoFilter Field:=2, Operator:=xlFilterValues
End If
End With
End Sub- alex_nMay 04, 2023Brass Contributor
I use Office 365. Mine is not progressively filtering after the second comma (search). I will have to keep searching ways to use one search field. If anyone has ideas please share. Thanks.
- byundtMay 02, 2023Brass ContributorI'm using Microsoft 365 Beta Channel. In testing yesterday before posting, the list progressively filtered as I added search terms separated by commas. Note that I did not hit Enter until entering the last search term.
It wouldn't surprise me if your experience is different. If so, you'll need to use Advanced Filter for the third and subsequent criteria. - alex_nMay 02, 2023Brass ContributorHi. Thanks for the response. The third criteria turns the search into a single search item, the result shows the list of the last entry as opposed to an AND array criteria result. With the first two (separated by comma) shows the result as expected but the third turns it into a single item search.
- byundtMay 01, 2023Brass Contributor
It's a bit clumsy, but I found you could enter as many "and" criteria as you like separated by commas using a For Each loop on the AutoFilter.
Private Sub TextBox1_Change() Dim xStr As String, xName As String Dim v As Variant, vv As Variant Dim xWS As Worksheet Dim xRg As Range On Error GoTo Err01 Application.ScreenUpdating = False xName = "Materials" xStr = TextBox1.Text Set xWS = ActiveSheet Set xRg = xWS.ListObjects(xName).Range If xStr <> "" Then v = Split(Application.Trim("*" & Replace(xStr, ",", "*,*") & "*"), ",") If IsArray(v) Then Select Case UBound(v) Case 0 xRg.AutoFilter Field:=2, Criteria1:="*" & v(0) & "*", Operator:=xlFilterValues Case 1 xRg.AutoFilter Field:=2, Criteria1:="*" & v(0) & "*", Criteria2:="*" & v(1) & "*", Operator:=xlFilterValues Case Else For Each vv In v xRg.AutoFilter Field:=2, Criteria1:="*" & vv & "*", Operator:=xlFilterValues Next End Select Else xRg.AutoFilter Field:=2, Criteria1:="*" & v & "*", Operator:=xlFilterValues End If Else xRg.AutoFilter Field:=1, Operator:=xlFilterValues End If Err01: End Sub - byundtMay 01, 2023Brass Contributor
AutoFilter allows one or two criteria with wildcards. See code below.
To get more, you need to use Advanced Filter as described at https://stackoverflow.com/questions/49858980/auto-filter-with-more-than-2-criteria-with-wildcards
Option Explicit Private Sub TextBox1_Change() Dim xStr As String, xName As String Dim v As Variant, vv As Variant Dim xWS As Worksheet Dim xRg As Range On Error GoTo Err01 Application.ScreenUpdating = False xName = "Materials" xStr = TextBox1.Text Set xWS = ActiveSheet Set xRg = xWS.ListObjects(xName).Range If xStr <> "" Then v = Split(Application.Trim("*" & Replace(xStr, ",", "*,*") & "*"), ",") If IsArray(v) Then Select Case UBound(v) Case 0 xRg.AutoFilter Field:=2, Criteria1:="*" & v(0) & "*", Operator:=xlFilterValues Case Else xRg.AutoFilter Field:=2, Criteria1:="*" & v(0) & "*", Criteria2:="*" & v(1) & "*", Operator:=xlFilterValues End Select Else xRg.AutoFilter Field:=2, Criteria1:="*" & v & "*", Operator:=xlFilterValues End If Else xRg.AutoFilter Field:=1, Operator:=xlFilterValues End If Err01: End Sub - alex_nMay 01, 2023Brass ContributorHere is the link per your request. Thanks.
https://1drv.ms/x/s!Aqv4zBSFNKaymmXkV1nE3Y-GyMoq?e=nDPv40 - byundtMay 01, 2023Brass ContributorPlease post a link to a sample workbook showing the code you are using..
- alex_nMay 01, 2023Brass ContributorTo expand on the same topic, how would I use the key word feature where the filter filters by the criteria where more than one word is in the criteria: search the column for the data that has key words "table", "napkin", "metal". Limit to 3 key words is good enough. Thanks in advance.
- alex_nMay 01, 2023Brass ContributorAlbert, I ended up using your solution. Thanks for the help!
- byundtOct 22, 2021Brass Contributor
If you revert to event sub TextBox1_Change, you can trigger it by entering a value in its linked cell even if you cannot enter a value directly in the textbox.
Private Sub TextBox1_Change() Dim xStr As String, xName As String Dim xRg As Range Application.ScreenUpdating = False With TextBox1 xName = "Materials" xStr = .Text Set xRg = .Parent.ListObjects(xName).Range If xStr <> "" Then xRg.AutoFilter Field:=2, Criteria1:="*" & xStr & "*", Operator:=xlFilterValues Else xRg.AutoFilter Field:=2, Operator:=xlFilterValues End If End With End Sub - byundtOct 21, 2021Brass ContributorAfter further investigation, I found that ActiveX controls (I tested both textbox and combobox) cannot be selected in a secondary window. This behavior occurs in 64-bit Excel 2010, 2013 and Microsoft 365. I don't know if it occurs with 32-bit Excel.
I suspect Microsoft will consider this behavior a "feature" rather than a bug because it had the same annoying behavior in multiple Excel versions. Features don't get fixed.
When you use external monitors, they are displaying content in secondary windows. The primary window remains your laptop.
So you have two good choices: abandon the use of ActiveX controls or else make sure to access them in your laptop display. Many Excel MVPs recommend avoiding ActiveX controls because of their buggy behavior. - alex_nOct 21, 2021Brass ContributorThe ActiveX text box works as desired on a main display - it can be the laptop monitor or whichever display is set as the main display when extended monitors are used. In our case, we use 1 or 2 monitors attached to our laptops. In my case it works on my laptop display as I keep the laptop monitor as the main one and when I move the excel file to other two monitors it does not work, i.e. I don't see what I type in the text box.