简体   繁体   English

动态过滤器一直隐藏我的行 - Excel VBA

[英]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.它将使用DblClick事件。 Otherwise, the Change event will be triggered by any character you input.否则, Change事件将由您输入的任何字符触发。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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