2017-06-06 190 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