【发布时间】: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 错误