【问题标题】:AutoFilter used to work but now gives error 1004AutoFilter 以前可以工作,但现在给出错误 1004
【发布时间】:2021-03-22 07:31:32
【问题描述】:

所以,以前的代码可以正常工作。我之前测试了很多。但现在错误才刚刚开始发生。

我的目标是打开另一个工作簿(我们的甘特图数据库),应用 3 个不同的过滤器,然后在所有可见行搜索和特定值中循环并将它们粘贴到目标工作簿中。

我已经尝试添加代码以删除然后再次输入自动过滤器,我尝试将标准 2 中的单元格引用更改为值(即“2020 年 11 月 30 日”),但错误不断发生。我希望大家能帮助我,因为我在网上看到过,这是一个很难解决且非常具体的问题。

注释代码: 子甘特图()

    Dim i, limite As Long
    Dim Wko, Wkd As Workbook
    Dim WksO, WksP As Worksheet
    
    Set Wkd = Workbooks("MC Produção TOTAL.xlsb") 'my objective workbook
    Set WksP = Wkd.Worksheets("BD_Resumo") 'worksheet to paste some gathered database
    Set WksOp = Wkd.Worksheets("Operação") 'workshett where i control what's happening
    
    Application.DisplayAlerts = False
    Workbooks.Open Filename:="M:\apoio_manutencao\03 - Relatórios\2 - Custos\Banco de Dados PA.*", ReadOnly:=True 'open the database workbook
    Application.DisplayAlerts = True
                         
    Set Wko = Workbooks(Workbooks.Count)
    Set WksO = Wko.Worksheets("Banco de Dados") 'where the data comes from

    i = 5

    If ActiveSheet.FilterMode Then
    WksO.ShowAllData 'here I make sure there is no filter applied, but autofilter is still there to be used
    End If

    limite = WksO.Range("A" & Rows.Count).End(xlUp).Row 'pick the last row with values on my database workbook

    '!!!!ERROR HAPPENS HERE!!!

    WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=21, Operator:= _
    xlFilterValues, Criteria2:=Array(1, WksOp.Range("K2").Text)                        'adds filter by date (in the range i have the value of the last day of the month 11/30/2020)
    
    '!!!!YEP, RIGHT ABOVE, ON THE FIRST FILTER!!!

    WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=15, Criteria1:= _
    "Produção"                                                                          'Add the second filter
    WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=2, Criteria1:=Array( _
    "Emblistamento", "Encapsulamento", "Envase"), Operator:=xlFilterValues              'add the third filter

    While WksP.Cells(i, 1) <> "" 'i have a group of TAGs that I want to use as final decision for gathering the info I want

        WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=5, Criteria1:= _
            WksP.Cells(i, 1).Value  'adds the last filter, the TAG I want to see at the moment
        WksP.Cells(i, 7).Value = 0

 For Each Cell In WksO.Range(Cells(2, 22), Cells(limite, 22)) 
        If Cell.Rows.Hidden = False And IsNumeric(Cell.Value) Then 'it will only work with visible cells that contain numeric values
                WksP.Cells(i, 7).Value = WksP.Cells(i, 7).Value + Cell.Value * 24 'i made previous sure that my objective cell is empty and than i can add the new values. I multiply because i am talking in decimal hours
        End If
 Next
    i = i + 1
    Wend

    WksP.Range("G5:G32").NumberFormat = "0.00"     'just to make sure my decimal numbers have only 2 numbers afeter "."
    Wko.Close SaveChanges:=False 'I don't want to mess with my original archive

    End Sub

我知道这是一个大代码,但我也不知道还能做什么。感谢您的宝贵时间。

【问题讨论】:

  • 可能只是一个错字:您不能将Criteria2 设置为过滤条件,您应该使用Criteria1。 Criteria2 仅用于过滤器之间。
  • 问题是,这是用宏记录器制作的(除了单元格引用,这是我改变的,宏记录器用日期代替)。它曾经以我发布的这种方式工作。我之前已经尝试过更改它,但为了以防万一,我又做了一次。通过这样做,程序不会出错。但它也一无所获。为了弄清楚发生了什么,我让代码在此代码行之前停止并逐步运行。过滤器隐藏了我所有的数据,我想criteria2 是正确的。不过谢谢你的建议。
  • 我认为您应该将criteria2 更改为criteria1。但是您是否尝试过WksOp.Range("K2").Value (.Value2) 或CStr(WksOp.Range("K2").Value) 而不是WksOp.Range("K2").Text 和/或"1" 而不是1?请注意,这个 WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=2, Criteria1:=Array( _ "Emblistamento", "Encapsulamento", "Envase"), Operator:=xlFilterValues 正在工作。
  • 我将在所有组合尝试下方发布尝试您的建议@VBasic2008。不幸的是,两者都没有奏效。 Criteria2 的问题在于它在按日期过滤时用作唯一标准。我正在尝试按月过滤。我们可以在这里看到更多细节link。 我的尝试及其结果: 第一次尝试:WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=21, Operator:= xlFilterValues, Criteria1:=Array("1", WksOp.Range("K2").Value) 结果:隐藏一切
  • 继续尝试 第二个:WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=21, Operator:= _ xlFilterValues, Criteria2:=Array("1", WksOp.Range("K2").Value) 结果:返回相同的 1004 错误 第三个:WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=21, Operator:= _ xlFilterValues, Criteria2:=Array(1, WksOp.Range("K2").Value) 结果:相同的 1004 错误第四个:WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter Field:=21, Operator:= _ xlFilterValues, Criteria2:=Array(1, CStr(WksOp.Range("K2").Value)) 结果:相同的 1004 错误

标签: excel vba


【解决方案1】:

自动筛选:日期标准 2 - 国际问题

标准2

  • Microsoft Docs about Criteria2 of Range.AutoFilter:
    第二个条件(字符串)。与 Criteria1 和 Operator 一起使用以构建复合标准。 也用作按日期、月份或年份过滤日期字段的单一标准。后面是一个详细说明过滤数组(级别,日期)的数组。其中 Level 为 0-2(年、月、日),Date 是过滤周期内的一个有效日期。

解决方案

WksO.Range(Cells(1, 1), Cells(limite, 41)).AutoFilter _
    Field:=21, _
    Operator:=xlFilterValues, _
    Criteria2:=Array(1, Format(WksOp.Range("K2").Value, "m\/d\/yyyy"))

两个例子

Option Explicit

Sub testOneDate()
    
    Dim rng As Range
    Set rng = Range("A1").CurrentRegion
    Dim mDate As Date: mDate = DateSerial(2020, 2, 1)
    Dim usDateString As String: usDateString = Format(mDate, "m\/d\/yyyy")
    
    Debug.Print rng.Address
    Debug.Print mDate
    Debug.Print usDateString
        
    rng.Worksheet.AutoFilterMode = False
    ' Filter by month of Date (February):
    rng.AutoFilter _
        Field:=2, _
        Operator:=xlFilterValues, _
        Criteria2:=Array(1, usDateString)

End Sub

Sub testTwoDates()
    
    Dim rng As Range
    Set rng = Range("A1").CurrentRegion
    Dim FirstDate As Date: FirstDate = DateSerial(2020, 2, 1)
    Dim SecondDate As Date: SecondDate = DateSerial(2020, 4, 1)
    Dim usFirst As String: usFirst = Format(FirstDate, "m\/d\/yyyy")
    Dim usSecond As String: usSecond = Format(SecondDate, "m\/d\/yyyy")
    
    Debug.Print rng.Address
    Debug.Print FirstDate, SecondDate
    Debug.Print usFirst, usSecond
        
    rng.Worksheet.AutoFilterMode = False
    ' Filter by months of Dates (February and April):
    rng.AutoFilter _
        Field:=2, _
        Operator:=xlFilterValues, _
        Criteria2:=Array( _
            1, Format(FirstDate, "m\/d\/yyyy"), _
            1, Format(SecondDate, "m\/d\/yyyy"))
 
End Sub

【讨论】:

  • 不幸的是它没有解决。我什至在第一次失败后尝试了一些变化。甚至尝试让我的手机将日期显示为“m/d/yyyy”。没有改变。我找到了一种不用这个自动过滤器来做我的逻辑的方法。完成需要多花 5 分钟,但我做到了。最后,在我的“对于每个单元格”中,有一个如果,我添加了一些“和”,它的结尾是:If cell.Rows.Hidden = False And IsNumeric(cell.Value) And Year(WksO.Cells(cell.Row, 21).Value) = Year(WksOp.Range("K2").Value) And Month(WksO.Cells(cell.Row, 21).Value) = Month(WksOp.Range("K2").Value) Then。我仍然接受新想法:D
  • 要使我的解决方案起作用,您必须在K2 中找到一个日期。您可以在新工作簿中尝试测试程序。只需写一些标题,将 2 月和 4 月的一些日期写到工作表的B 列,然后用一些虚拟数据填充其余部分。然后转到 VBE 并运行 testOneDate 和 testTwoDates 程序。我将不胜感激。
  • 是的,我确定我的 K2 值是一个日期。我还在第 1 列和第 3 列(“A”和“C”)以及第 2 列(“B”)我只输入日期,在一年中的所有月份中变化。它仍然返回相同的 1004 错误。我不知道你是否检查过这个link,第一个答案是关于 vba 和 excel 中可能存在的错误,其中某些事情只能通过用户界面完成。即使您尝试使用宏记录器,它也会返回错误。
  • 问题是它对我有用。这是Google Drive 上的my file 的链接。你能分享一下你正在使用的Windows 和Office 的版本吗?我正在使用Windows 10 64-bit 和Office 2019 32-bit。顺便说一句,我有类似的经历here。
  • 我正在使用Office Home and business 2019 32-bit 和Windows 10 64-bit。我的VBA(我想是同一个buuut)version 1104, retail 7.1.1104, Forms3 16.0.13127.20204。无论如何,只要真正理解“选项显式”的含义,我就会开始使用它。我喜欢宣布一切,但有些事情可能会从我的脑海中消失。不幸的是,我下载了您的存档并运行它,并且发生了同样的错误。我的 vba 是否可能缺少某些库?
猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 2018-02-10
相关资源
最近更新 更多