2017-05-29 58 views
1

我正在嘗試編寫代碼。我已經應用了一個過濾器,然後我需要在幾行之後再應用一個過濾器。但第二個過濾器沒有得到應用。這裏是我的代碼 -如何應用另一個過濾器,如果已經在VBA中應用了一個過濾器

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

你怎麼知道它沒有得到應用?當您轉到工作表並單擊已過濾的列時,它只顯示一個標準? – Masoud

+0

@Masoud是的,你是對的。它只顯示第一個過濾器。 –

回答

1

的問題是在這裏:

If ActiveSheet.AutoFilterMode = False Then ... 

在這裏,你檢查AutoFilterMode是假的,而你已經在前面的線所應用的濾波器。所以它進入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

相關問題