【问题标题】:Conditional statement for Excel VBA codeExcel VBA 代码的条件语句
【发布时间】:2017-06-07 00:52:08
【问题描述】:

我需要一点帮助。让我们从代码开始:

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

我需要为上述代码创建一个条件语句,如果没有匹配 Criteria1 的文本 (W1793-PAS (Agency)),它将停止该过程。如果其中没有包含 W1793-PAS (Agency) 的行,我不希望它运行。

【问题讨论】:

    标签: excel vba


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      相关资源
      最近更新 更多