使用自动过滤器时运行时错误424

时间:2017-07-18 16:06:59

标签: excel vba excel-vba

我认为我有一个范围问题,因为我的变量是在另一个模块中定义的。 WholeSheetRange发生错误。我尝试将其定义为全局变量,但后来得到一个没有设置错误的对象91.然后我尝试在我的例程中本地定义一个新范围,然后我的自动过滤器不起作用。没有找到任何记录。

这是我的代码:

Sub date_entered_exit(ByVal Cancel As MSForms.ReturnBoolean)
    datevar = inv_list.date_entered
    Dim Rng As Range
    'Dim date_entered As Date
    'date_entered = "13/07/2017"
    'date_entered = Format(inv_list.date_entered, "mm/dd/yyyy")

    If Not IsDate(datevar) Then
        MsgBox "Input must be a date in the format: 'mm/dd/yyyy'"
        Cancel = True
        Exit Sub
    End If

    Set Rng = Range("A:A").Find(datevar)
        Debug.Print datevar
    If Rng Is Nothing Then
        MsgBox "Input a date within the range"
    Else
        WholeSheetRange.AutoFilter Field:=1, Criteria1:="=datevar"
    End If

End Sub

这是我的代码,其中定义了WholeSheetRange

'Declariations to select entire worksheet range
Dim sht As Worksheet
Dim BigLastRow, LastRow As Long
Dim LastColumn As Long
Dim StartCell, WholeSheetRange As Range 'WholeSheetRange

Set sht = Worksheets("Unfiltered_Flight_Schedule")
Set StartCell = Range("A2")

'turn on autofilter and clear filter if there is one
If Not ActiveSheet.FilterMode Then
    ActiveSheet.Range("A1").AutoFilter
Else
    ActiveSheet.ShowAllData
End If

BigLastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
Set WholeSheetRange = Range("A1:M" & BigLastRow)

此代码在日期检查之前发生,然后以下列方式使用:

If stn_DEN.Value = True Then
    WholeSheetRange.AutoFilter Field:=2, Criteria1:="=DEN"
ElseIf stn_SFO.Value = True Then
    WholeSheetRange.AutoFilter Field:=2, Criteria1:="=SFO"
End If

0 个答案:

没有答案