复制自动筛选的数据VBA-块未设置错误

时间:2019-05-10 18:11:07

标签: excel vba

我试图建立一个宏,该宏将过滤数据并将其从表复制到另一个选项卡。我正在使用此guide,但遇到了问题。在With ActiveWorkbook.Sheets("Quote").AutoFilter.Range行上,抛出一个错误,指出未设置with块。既然那是该行试图做的事情,我对为什么感到困惑。任何帮助将不胜感激。

Sub CopyFilteredData()
Dim rng As Range
Dim autofiltrng As Range

    With ActiveSheet

        .Range("A2").AutoFilter field:=8, Criteria1:="Issued"

    End With


    With Activesheet.AutoFilter.Range
        On Error Resume Next
            Set autofiltrng = .Offset(1, 0).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
        On Error GoTo 0

    End With

    If autofiltrng Is Nothing Then

    MsgBox "No Data Available."

    Else
        Worksheets("Sheet2").Cells.Clear
            Sheet2.Range("A1") = "Policy Holder"
            Sheet2.Range("B1") = "Month"
            Sheet2.Range("B1") = "Week"
            Sheet2.Range("B1") = "Product Type"
            Sheet2.Range("B1") = "Total Items"
            Sheet2.Range("B1") = "Source of Sale"
            Sheet2.Range("B1") = "NB Premium"
            Sheet2.Range("B1") = "Status"
            Sheet2.Range("B1") = "Notes"
        Set rng = ActiveSheet.AutoFilter.Range
        rng.Offset(1, 0).Resize(rng.Rows.Count - 1).Copy Destination:=Worksheets("Sheet2").Range("A2")
    End If

ActiveSheet.ShowAllData
End Sub

0 个答案:

没有答案