我正在尝试运行一个宏,该宏将从 Excel 中的表中提取数据,而表最初可能会被过滤。
代码应该:
我想使用列标题名称:“STATUS”,因为表已经更改了几次,索引也可能会更改。
通过各种研究和尝试,我认为我有正确的行可以清除过滤器而不从表中删除过滤器。
但是,我现在在步骤 2 中收到:“Range 类的 AutoFilter 方法失败”:
Sub OpenPOs()
Dim wbInventory As Workbook
Set wbInventory = Workbooks("IWI Inventory MASTER Report.xlsb")
With wbInventory
With wsPOTracker 'This is a Code name for worksheet within the workbook
'Clear the Table Filter
.ListObjects("Table1").AutoFilter.ShowAllData
'Apply Table Filter to the Column: "STATUS" Criteria = "O"
.ListObjects("Table1").ListColumns("STATUS").Range.AutoFilter , Criteria1:="O" '<Run-time error '1004': AutoFilter method of Range class failed
'Rest of the code
我尝试研究/复制的链接:
试试这个:
Dim lo As ListObject
Set lo = wsPOTracker.ListObjects("Table1") 'reference the listobject/table
If Not lo.AutoFilter Is Nothing Then lo.AutoFilter.ShowAllData
lo.Range.AutoFilter Field:=lo.ListColumns("STATUS").Index, Criteria1:="O"