2017-06-06 68 views
0

我需要一點幫助。讓我們先從代碼:Excel VBA代碼的條件語句

Range("a1").AutoFilter Field:=1, Criteria1:="W1793-PAS (Agency)" 
'exclude 1st row (titles) 
With Intersect(Range("a1").CurrentRegion, _ 
       Range("2:60000")).SpecialCells(xlCellTypeVisible) 
    .Rows.Delete 
End With 
ActiveSheet.ShowAllData 
If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False 

我需要爲上述代碼,如果沒有文字匹配標準1(W1793-PAS(局)),將停止該過程的條件語句。如果W1793-PAS(Agency)沒有行,我不希望它運行。

回答

1

只需檢查Autofilter通過存儲在一個Range對象的 過濾範圍返回任何東西或不然後將其刪除

Dim rng As Range 

'Remove any filters 
ActiveSheet.AutoFilterMode = False 

With Range("A1") 
    .AutoFilter Field:=1, Criteria1:="W1793-PAS (Agency)" 
    '~~> Set this here 
    Set rng = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow 
End With 

'Remove any filters 
ActiveSheet.AutoFilterMode = False 

'~~> Check if there were any filtered results and then delete 
If Not rng Is Nothing Then rng.Delete