VBAfilter(search整数值)

我有下面的代码基于什么input到文本框中的filter。 这适用于string,但它不适用于整数search。 任何想法我可能做错了什么?

Private Sub TextBox1_Change() On Error Resume Next metin = TextBox1.Value Set bul = Range("a4:a10").Find(What:=metin) Application.Goto Reference:=Range(bul.Address), Scroll:=False Selection.AutoFilter field:=1, Criteria1:=TextBox1.Value & "*" If metin = "" Then Selection.AutoFilter End If End Sub 

添加Range("a4:a10").NumberFormat = "@"开头。 用数字,Excel试图比较值,而不是string的数字表示。 因此,它试图完全匹配:)该行将数字序列视为string,并将应用string比较。 最终的代码是:

 Private Sub TextBox1_Change() Range("a4:a10").NumberFormat = "@" On Error Resume Next metin = TextBox1.Value Set bul = Range("a4:a10").Find(What:=metin) Application.Goto Reference:=Range(bul.Address), Scroll:=False Selection.AutoFilter field:=1, Criteria1:=TextBox1.Value & "*" If metin = "" Then Selection.AutoFilter End If End Sub 

为了优化,你应该在这个方法之外的地方设置范围格式,所以你不必每次都改变文本框。