【问题标题】:VBA: Output message box when Autofilter returns no dataVBA:当自动过滤器没有返回数据时输出消息框
【发布时间】:2017-09-25 16:00:14
【问题描述】:

如果过滤后有任何结果,我想将自动过滤的范围复制并粘贴到新工作表中,如果没有结果,则显示一个消息框。

但是,当我使用不会返回任何结果的过滤条件进行测试时,不会出现消息框(显示空白工作表)

    Dim WSNew As Worksheet
    Set WSNew = Worksheets.Add

    Dim rngVisible As Range
    Set rngVisible = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)

    If rngVisible.Rows.Count > 1 Or rngVisible.Areas.Count > 1 Then
        rngVisible.Copy
            With WSNew.Range("A1")
                .PasteSpecial Paste:=8
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
                Application.CutCopyMode = False
                .Select
            End With
    Else
        MsgBox ("No such filtered criteria")
    End If

【问题讨论】:

标签: vba excel


【解决方案1】:

首先您想在活动工作表中工作,但是当您执行 Worksheets.Add 时,添加的工作表可以成为活动工作表(我认为取决于 Excel 版本)。这可能是个问题。所以你必须设置一个 WSOld 并处理它。

此外,您的自动过滤功能顺序不正确(首先声明 Worksheet.Range(firstColumfirstLine : lastColumLastLine),然后对其进行自动过滤:https://msdn.microsoft.com/fr-fr/library/office/ff193884.aspx)。

您还必须选择过滤数据的条件。

然后使用 UsedRange.SpecialCells(xlCellTypeVisible) 设置过滤单元格的范围并在其上进行交互。

这对我有用:

 Dim WSOld As Worksheet
 Dim WSNew As Worksheet

'store the active sheet in WSOld to be sure that selection will be apply on it
Set WSOld = ActiveSheet
Set WSNew = Worksheets.Add

'select the range to apply the filter and choose criteria
WSOld.Range("A1:B6500").AutoFilter Field:=2, Criteria1:="te"

'select the data visible after filter
Dim rngVisible As Range
Set rngVisible = WSOld.UsedRange.SpecialCells(xlCellTypeVisible)

If rngVisible.Rows.Count > 1 Or rngVisible.Areas.Count > 1 Then
    rngVisible.Copy
        With WSNew
            .Range("A1").PasteSpecial Paste:=8
            .Range("A1").PasteSpecial xlPasteValues
            .Range("A1").PasteSpecial xlPasteFormats
            Application.CutCopyMode = False
            .Select
        End With
Else
    MsgBox ("No such filtered criteria")
End If

'remove autofilter
WSOld.Range("A1:B6500").AutoFilter

希望对你有帮助。

【讨论】:

    【解决方案2】:

    请检查:

    Option Explicit
    Sub Filter_range()
    
    
      Dim WSNew As Worksheet
      Dim rngVisible As Range
    
    
    
    
        Set rngVisible = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible)
    
        If rngVisible.Rows.Count > 1 Or rngVisible.Areas.Count > 1 Then
            rngVisible.Copy
    
            Set WSNew = Worksheets.Add
    
                With WSNew.Range("A1")
                    .PasteSpecial Paste:=8
                    .PasteSpecial xlPasteValues
                    .PasteSpecial xlPasteFormats
                    Application.CutCopyMode = False
                    .Select
                End With
        Else
            MsgBox ("No such filtered criteria")
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-04
      • 2017-01-15
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      相关资源
      最近更新 更多