2015-05-19 316 views
0

我嘗試了以下過濾(篩選器之間)基於2個值的工作表。這兩個值在用戶表單'A1_Filter_Range'的textbx1和textbox2中輸入。Excel VBA:使用文本框值的自動篩選

但代碼不能正常工作......我得到的錯誤: 運行時error'424「:所需的對象」

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

任何想法? 感謝SMORF

+0

你的代碼是有一個很難找到的文本框父窗體。解決方案將取決於上述代碼段的位置。 – Jeeped

回答

0

簡單的解決方案,你一定要告訴Excel中哪裏去找TextBox ES:

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

如果文本框放在另一個工作表上而不是當前工作表上,請替換ActiveSheet。與工作表(#)。其中#是工作表索引。 – Physikbuddha

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 
+1

你能向作者解釋他的代碼有什麼問題嗎? –