我正在尝试编写代码。我已经应用了一个过滤器,然后我需要在几行之后再应用一个过滤器。但第二个过滤器没有得到应用。这是我的代码 -
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
答案 0 :(得分:1)
问题在于:
If ActiveSheet.AutoFilterMode = False Then ...
在此处检查在前一行中应用过滤器时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
。