如果已经在VBA中应用了一个filter,如何应用另一个filter

我试图写一个代码。 我已经应用了一个filter,然后我需要在几行之后再应用一个filter。 但第二个filter没有得到应用。 这是我的代码 –

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1) End Function Sub occ_const_ashish() Dim wb As Worksheet Dim bldscrng As Range Dim wb1 As String Dim i As String, j As String Dim arr() As Variant Dim arrTemp() As Variant Set wb = Sheets(ActiveSheet.Name) wb1 = ActiveSheet.Name wb.Activate LC = Sheets(wb1).Cells(1, Columns.Count).End(xlToLeft).Column ' Sets the search range as A1 to the last column with a header on the Run sheet Set sRange = Sheets(wb1).Range("A1", Cells(1, LC)) ' With the search range With sRange ' Set Rng as the cell where "Country" is found Set cntryrng = .Find(What:="CNTRYCODE", After:=.Cells(1), _ LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False) If Not cntryrng Is Nothing Then ' Define LastRow as the last row of data under the Due Date header LR = Sheets(wb1).Cells(Rows.Count, cntryrng.Column).End(xlUp).Row ' Copy from the Due Date header down to the last row of that column and paste to A1 of Paste Report Here sheet 'Set rngSourceRange1 = Sheets(wb1).Range(cntryrng(2), Cells(LR, cntryrng.Column)) Set rngSourceRange1 = Sheets(wb1).Range(cntryrng(2), Cells(LR, cntryrng.Column)) For Each cell In rngSourceRange1 i = cell.Value rw = cell.Row 'MsgBox i With ThisWorkbook.Sheets("Construction") arr = Application.Transpose(.Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 1)).Value2) End With 'arr1 = Application.Transpose(Sheets(wb1).Range(Sheets(wb1).Cells(2, 5), Sheets(wb1).Cells(Sheets(wb1).Cells(Sheets(wb1).Rows.Count, 5).End(xlUp).Row, 5)).Value2) If IsInArray(i, arr) Then 'arrayTemp = Filter(arr1, i) 'MsgBox Join(arrayTemp, ",") With ThisWorkbook.Sheets("Construction") .AutoFilterMode = False .Range("A1:E1").AutoFilter .Range("A1:E1").AutoFilter Field:=1, Criteria1:=i End With With sRange ' Set Rng as the cell where "Country" is found Set bldscrng = .Find(What:="BLDGSCHEME", After:=.Cells(1), _ LookIn:=xlValues, LookAt:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False) col1 = bldscrng.Cells(1, 1).Column j = Cells(rw, col1).Value If j = "" Then Cells(rw, LC + 1).Value = "BLDSCHEME is BLANK" 'MsgBox "bldscheme is blank" Else 'MsgBox j With ThisWorkbook.Sheets("Construction") arr1 = Application.Transpose(.Range(.Cells(2, 3), .Cells(.Cells(.Rows.Count, 3).End(xlUp).Row, 3)).Value2) End With If IsInArray(j, arr1) Then 'MsgBox "scheme found" With ThisWorkbook.Sheets("Construction") If ActiveSheet.AutoFilterMode = False Then Range("A1:E1").AutoFilter .Range("A1:E1").AutoFilter .Range("A1:E1").AutoFilter Field:=1, Criteria1:=i .Range("A1:E1").AutoFilter Field:=3, Criteria1:=j End With Else 'MsgBox "scheme not found" Cells(rw, LC + 1).Value = "BLDSCHEME is INVALID" End If End If End With Else MsgBox "Country not found" End If Next cell End If End With End Sub 

问题在这里:

 If ActiveSheet.AutoFilterMode = False Then ... 

在这里,您检查在前面几行中应用filter时AutoFilterMode是否为false。 所以它去了Else部分,并显示: MsgBox "scheme not found"

修改这部分代码如下,以理解我的意思:

  With ThisWorkbook.Sheets("Construction") .AutoFilterMode = False Debug.Print .AutoFilterMode 'before applying autofilter .Range("A1:E1").AutoFilter Debug.Print .AutoFilterMode 'after applying autofilter .Range("A1:E1").AutoFilter Field:=1, Criteria1:=i End With 

另外,当你想使用这么多if语句试图使缩进清晰,并有一些评论(也许编号),使您的代码易读。 而且,你可以考虑使用Select Case