0

在单个宏中,我试图选择一个范围,命名它,然后在自动过滤数据时引用该范围。这是我的代码:

'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'的方法'范围'失败”,并且调试器将第二个代码块的第一行突出显示为罪魁祸首。我无法在网络上找到像这样的过滤数据的任何示例,因此我将不胜感激任何关于我做错了什么的见解。谢谢!

4

3 回答 3

3
DataDump.Range("myrange").AutoFilter field:=11, Criteria1:="<>""PDC1"""

应该

myrange.AutoFilter field:=11, Criteria1:="<>""PDC1"""
于 2013-10-15T20:51:39.923 回答
3

myrange 是范围而不是字符串。因此替换:

DataDump.Range("myrange").AutoFilter field:=11, Criteria1:="<>""PDC1"""

myrange.AutoFilter field:=11, Criteria1:="<>""PDC1"""
于 2013-10-15T20:54:11.933 回答
0

为了后代,我向您展示我更新的代码。它包括替换所有.Select语句。

Set bDataDump = ActiveWorkbook
Set DataDump = bDataDump.Sheets(1)
Set myrange = DataDump.Range("A1")
Set myrange = Range(myrange, myrange.End(xlToRight))
Set myrange = Range(myrange, myrange.End(xlDown))
'
myrange.AutoFilter field:=12, Criteria1:="<>PDC1"
Set dltrng = DataDump.Range("A2")
Set dltrng = rnage(dltrng, dltrng.End(xlDown))
dltrng.EntireRow.Delete
DataDump.ShowAllData

我觉得很奇怪,录制的宏的过滤器部分需要双引号

ActiveSheet.Range("$A$1: ... Criteria1:="<>""PDC1"""

但以命名范围开头时需要单引号(参见上面的第 7 行)。

于 2013-10-16T19:41:30.680 回答