简体   繁体   中英

Dynamic filter keeps hiding my rows - Excel VBA

The moment I type in the text box my rows become hidden and don't come back after clearing the text box. I used the same exact code on a previous table in a different sheet and it worked flawlessly. I believe the error stems from me copying and pasting the entire table into a different sheet and tweaking the code for the new table. I checked on the linked cell and made sure to change the table name. What am I missing?

Private Sub TextBox1_Change()

Application.ScreenUpdating = False

ActiveSheet.ListObjects("Customers").Range.AutoFilter Field:=1, Criteria1:=[C2] & "*", Operator:=xlFilterValues

Application.ScreenUpdating = True

End Sub

Try this code, please. It will use DblClick event. Otherwise, the Change event will be triggered by any character you input. Now, after setting the filter criteria in the text box, you should double click inside it :

Private Sub TextBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean
 Dim Tb As ListObject

 Set Tb = ActiveSheet.ListObjects("Customers")
 Application.ScreenUpdating = False
  If TextBox1.Text = "" Then
    Tb.AutoFilter.ShowAllData
  Else
    Tb.Range.AutoFilter field:=1, Criteria1:=CStr([C2]), Operator:=xlFilterValues
  End If
 Application.ScreenUpdating = True
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM