Autofilter xlFilterAllDatesInPeriod"month" 运行突然出错

问题描述 投票:0回答:1

我写了一个运行了几个月的代码,突然,昨天它停止运行了。 我对导致问题的原因感到困惑。

自动过滤器发生错误。 我收到运行时错误“1004”Range 类的 AutoFilter 方法失败

我也尝试在给定月份运行宏 Criteria1:=xlFilterAllDatesInPeriodFebruary,运算符:=xlFilterDynamic 但它不适用于二月,但宏观适用于任何其他月份。我真的觉得很奇怪。

谁能帮我解决这个问题,我对此感到非常沮丧。 :S 下面只是宏的摘录,我删除了所有不必要的内容(我也尝试运行它,但仍然收到错误消息)。

Sub Autofilter()

Dim ws1 As Worksheet
Dim strUserName As String
strUserName = Environ("Username")

Dim xlmonth As Long
Dim sd As Variant
sd = Workbooks("Macro Launcher.xlsm").Worksheets("Main Sheet").Range("B2") 'contains current month

If sd = "January" Then xlmonth = xlFilterAllDatesInPeriodJanuary _
Else If sd = "February" Then xlmonth = xlFilterAllDatesInPeriodFebruary _
Else If sd = "March" Then xlmonth = xlFilterAllDatesInPeriodMarch _
Else If sd = "April" Then xlmonth = xlFilterAllDatesInPeriodApril _
Else If sd = "May" Then xlmonth = xlFilterAllDatesInPeriodMay _
Else If sd = "June" Then xlmonth = xlFilterAllDatesInPeriodJune _
Else If sd = "July" Then xlmonth = xlFilterAllDatesInPeriodJuly _
Else If sd = "August" Then xlmonth = xlFilterAllDatesInPeriodAugust _
Else If sd = "September" Then xlmonth = xlFilterAllDatesInPeriodSeptember _
Else If sd = "October" Then xlmonth = xlFilterAllDatesInPeriodOctober _
Else If sd = "November" Then xlmonth = xlFilterAllDatesInPeriodNovember _
Else If sd = "December" Then xlmonth = xlFilterAllDatesInPeriodDecember

Workbooks.Open ("C:\Users\" & strUserName & "\Downloads\DNR BIP 1_ Detail Employee Listing - Active Employee Information.xlsx")
Range("B:C,G:H,J:O,R:W,AA:AL,AQ:AW,AY:AY,BA:BG").Delete Shift:=xlToLeft
Set ws1 = Workbooks("DNR BIP 1_ Detail Employee Listing - Active Employee Information.xlsx").Sheets("Sheet1")
ws1.Range("$A:$P").Autofilter field:=10, Criteria1:="HR" '- I tried removing this one as well, still got the error
'ws1.Range("$A:$P").AutoFilter Field:=11, Criteria1:=xlFilterAllDatesInPeriodFebruary, Operator:=xlFilterDynamic -- trited this as well, did not work

ws1.Range("$A:$P").Autofilter field:=11, Criteria1:= _
       xlmonth, Operator:=xlFilterDynamic '- this is the one where the macro fails

End Sub

enter image description here

我尝试用 ActiveSheet 替换 ws1,似乎没有任何效果。 如果有人能帮助我,我真的很感激。

PS:我不是专业人士,只是初学者

vba filter dynamic macros autofilter
1个回答
0
投票

February 在 VBA 编码中拼写错误(不确定是有意还是无意),但从技术上讲,我必须将 February 更改为 Februray 才能使代码正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.