Excel VBA:使用文本框值自动筛选

时间:2015-05-19 18:51:43

标签: excel vba excel-vba

我尝试使用以下内容根据2个值过滤(在过滤器之间)我的工作表。在userform'A1_Filter_Range'中的textbx1和textbox2中输入2个值。

但代码不起作用......我收到错误: 运行时错误'244':需要对象'

May 19 20:50:51 myname.local postfix/postdrop[18672]: warning: mail_queue_enter: create file maildrop/744151.18672: Permission denied

有什么想法吗? 谢谢SMORF

2 个答案:

答案 0 :(得分:0)

简单的解决方案,你必须告诉Excel在哪里寻找TextBox es:

ActiveSheet.Range("$A$1:$AD$2000").AutoFilter Field:=17, _
    Criteria1:=ActiveSheet.TextBox1.Value, _
    Operator:=xlAnd, _
    Criteria2:=ActiveSheet.TextBox2.Value

答案 1 :(得分:0)

If TextBox1.Text <> "" Then

        TextBox1.BackColor = RGB(254, 254, 22) 'yellow

        Dim word As String


        word = "*" & TextBox1.Text & "*"
        Selection.AutoFilter Field:=1, Criteria1:=word, Operator:=xlAnd

    Else
        Selection.AutoFilter Field:=1
        TextBox1.BackColor = RGB(55, 255, 255) 'cyan

    End If
            'ActiveSheet.Protect Password:=""

End Sub

Private Sub TextBox2_Change()
                'ActiveSheet.Unprotect Password:=""

 If TextBox2.Text <> "" Then

        TextBox2.BackColor = RGB(254, 254, 22) 'yellow

        Dim slovo As String


        word = "*" & TextBox2.Text & "*"
        Selection.AutoFilter Field:=2, Criteria1:=word, Operator:=xlAnd

    Else
        Selection.AutoFilter Field:=2
        TextBox2.BackColor = RGB(55, 255, 255) 'cyan

    End If
                'ActiveSheet.Protect Password:=""
End Sub