用于在多张纸上过滤的vba代码

时间:2015-10-27 11:06:38

标签: excel-vba vba excel

Sub Filter()
Dim j As Integer, k As Integer
j = Worksheets.Count
For k = 1 To j
With Worksheets(k)
.Range("A1").AutoFilter field:=1, Criteria1:="tatamotors"
.Range("A1").AutoFilter field:=2, Criteria1:="29-oct-15"
End With
Next k
End Sub

以上代码在网络搜索中找到,并根据我的需要进行了小修改。它工作正常。需要专家的帮助。我一直在autofilter字段1中更改不同的名称,即有时候想要过滤掉ACC,有时候需要RELIANCE等等。专家可以在这里帮助我,而不是在代码中进行更改,显示输入框?

2 个答案:

答案 0 :(得分:0)

是的,想法是在eimput框上使用来定义过滤器密钥:

Sub Filter_Click()
Dim j As Integer, k As Integer
j = Worksheets.Count
Dim key1 As String
Dim key2 As String
key1 = InputBox("Type your key search", "Title")
key2 = InputBox("Type your key search", "Title")
For k = 1 To j
With Worksheets(k)
.Range("A1").AutoFilter field:=1, Criteria1:=ky1
.Range("A1").AutoFilter field:=2, Criteria1:=ky2
End With
Next k
End Sub

另一个可以使用带有一个选择框的表单来选择可用的条件,否则您可以键入数据范围中缺少的一个值

答案 1 :(得分:0)

感谢Fabrizio,我根据自己的需要做了一些改动,现在工作正常。修改后的代码如下。

Sub Filter_Click()
Dim j As Integer, k As Integer
j = Worksheets.Count
Dim key1 As String
key1 = InputBox("Type Script Name", "Title")
For k = 1 To j
With Worksheets(k)
.Range("A1").AutoFilter field:=1, Criteria1:=key1
.Range("A1").AutoFilter field:=2, Criteria1:="29-oct-15"
End With
Next k
End Sub