简体   繁体   English

在vba中的功能中使用自动筛选

[英]Using Autofilter in Function in vba

The below function is throwing error 424 Object Required . 下面的函数抛出错误424 Object Required

I want to use this in Worksheets asformula. 我想在工作表格式中使用它。

Data is available in Uploaded Report tab. 数据在“上载的报告”选项卡中可用。 Row 7 is the Header row . 第7行是Header行。

Function Bookings(Start_date As Date, End_date As Date) As Long
    On Error GoTo Protection
    Dim l_row As Long
    Dim rngRow, resultRng, filterRng As Range
    Bookings = 0
    l_row = Worksheets("Uploaded Report").Cells(Rows.Count, 1).End(xlUp).Row

    Worksheets("Uploaded Report").AutoFilterMode = False

    Set filterRng = Worksheets("Uploaded Report").Range("A7:E" & l_row)
    filterRng.AutoFilter field:=1, Criteria1:="GC Hi Top", VisibleDropDown:=True
    'Worksheets("Uploaded Report").Range("A7:E" & l_row).AutoFilter Field:=3, Criteria1:=">=" & Format(Start_date, "mm/dd/yyyy"), Operator:=xlAnd, Criteria2:="<=" & Format(End_date, "mm/dd/yyyy")
    Worksheets("Uploaded Report").Activate
    Set resultRng = filterRng.AutoFilter.Range.SpecialCells(xlCellTypeVisible)

    For Each rngRow In resultRng
        If rngRow.Row = 7 Then
           GoTo NextIteration
        End If
        If Len(Worksheets("Uploaded Report").Range("A" & rngRow.Row).Value) > 0 Then
            If rngRow.Row > 1 And rngRow.Column = 3 Then
                Bookings = Bookings + 1
            End If
        End If

NextIteration: Next rngRow NextIteration:下一个rngRow

Protection: MsgBox Err.Number & Err.Description 保护:MsgBox错误号和错误描述

End Function

How to get rid of the error 如何摆脱错误

When testing your code, the error was raised by the line: 在测试代​​码时,该错误由以下行引起:
Set resultRng = filterRng.AutoFilter.Range.SpecialCells(xlCellTypeVisible)

Change that line to : 将该行更改为:
Set resultRng = filterRng.SpecialCells(xlCellTypeVisible)


Other things 其他事情

  1. You have incorrectly declared variables i presume. 我认为您有错误声明的变量。

    Dim rngRow, resultRng, filterRng As Range
    change to 改成
    Dim rngRow As Range, resultRng As Range, filterRng As Range

  2. In the line for l_row , you are missing a parent for Rows.count l_row的行中,您缺少Rows.count的父Rows.count
    l_row = Worksheets("Uploaded Report").Cells(Rows.Count, 1).End(xlUp).Row
    change to 改成
    l_row = Worksheets("Uploaded Report").Cells(Worksheets("Uploaded Report").Rows.Count, 1).End(xlUp).Row
    or use With...End With statement. 或使用With...End With语句。

  3. Are you sure you want Error handler in function? 您确定要在函数中使用错误处理程序吗? Without it it will just display excel error in the cell, without the MsgBox, which for clueless users is useless and scary. 没有它,它将仅在单元格中显示excel错误,而没有MsgBox,这对于无知的用户来说是毫无用处和令人恐惧的。 If you opt to keep the Error handler, add a line Exit Function before the Protection: label. 如果选择保留错误处理程序,请在Protection:标签之前添加一行Exit Function

  4. To be more efficient and to get rid of the redundant If...End If statements you can set the resultRng to 为了提高效率并摆脱多余的If...End If语句,可以将resultRng设置为
    Set resultRng = filterRng.Columns(3).SpecialCells(xlCellTypeVisible)
    This way you will only loop through column "C". 这样,您将仅循环通过“ C”列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM