在单个宏中,我试图选择一个范围,命名它,然后在自动过滤数据时引用该范围。这是我的代码:
'Select and name range
Set bDataDump = ActiveWorkbook
Set DataDump = bDataDump.Sheets(1)
DataDump.Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range(Selection, Selection.End(xlDown)).Select
Set myrange = Selection
'AutoFilter that range for everything but PDC1 and delete visible rows
'Code fails on next line
DataDump.Range("myrange").AutoFilter field:=11, Criteria1:="<>""PDC1"""
ActiveCell.Range("A2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.EntireRow.Delete
DataDump.ShowAllData
当我运行宏时,我得到“对象'_Worksheet'的方法'范围'失败”,并且调试器将第二个代码块的第一行突出显示为罪魁祸首。我无法在网络上找到像这样的过滤数据的任何示例,因此我将不胜感激任何关于我做错了什么的见解。谢谢!