AdvanceFilter引发1004错误,对象定义错误

时间:2019-06-21 19:09:17

标签: excel vba

  

我遇到运行时错误'1004':当我的代码到达高级过滤器行时,应用程序定义或对象定义的错误。

我试图记录一个有效的宏,但是当我将范围调整为动态时,我开始收到错误消息。我还尝试按照提示仔细输入。

Private Sub LogOffenders()
    Dim Month As Integer
    Dim SID1001 As Range
    Dim MonSID  As Range
    Dim SIDRowCount As Long
    Dim OffCol As Integer
    Dim LasMon As Range
    Dim UniOff As Long
    Dim UniCount As Long

    'Count row
    SIDRowCount = Worksheets("1001").Cells(Rows.Count, "C").End(xlUp).Row - 1
    OffCol = Int(Format(Date, "mm"))

    Set SID1001 = Worksheets("1001").Range("C2:C" & SIDRowCount)
    Set MonSID = Worksheets("OffLog").Range(Worksheets("OffLog").Cells(2, OffCol), Worksheets("OffLog").Cells(SIDRowCount, OffCol))

    If OffCol = 1 Then
        Set LasMon = Worksheets("OffLog").Range(Worksheets("OffLog").Cells(2, 12), Worksheets("OffLog").Cells(SIDRowCount, 12))
    Else:
        Set LasMon = Worksheets("OffLog").Range(Worksheets("OffLog").Cells(2, OffCol - 1), Worksheets("OffLog").Cells(SIDRowCount, OffCol))
    End If

    'copy SIDs to SID log.
        SID1001.Copy
        MonSID.PasteSpecial Paste:=xlPasteValues, operation:=xlPasteSpecialOperationNone, skipblanks:=False, Transpose:=False

    'Count number of unique SIDs in copied column
        LasMon.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Worksheets("OffLog").Range("P"), unique:=True  '**Code breaks here***
        UniCount = Worksheets("1001").Cells(Rows.Count, "P").End(xlUp).Row

End Sub

我想复制P列中的唯一值,然后计算值的数量。

  

我收到运行时错误'1004':应用程序定义或对象定义的错误。

0 个答案:

没有答案