“提取范围未定义”:高级filter

我想在VBA中运行这个简单的代码,但它一直给我“提取范围没有定义”的错误:

Private Sub CommandButton1_Click() 'Dim rng As Range Dim RowLast As Long Dim perporig As Workbook count = 0 Set perporig = Workbooks.Open("\\Etnfps02\vol1\DATA\Inventory\Daily tracking\perpetual.xlsx", , ReadOnly) With perporig.Sheets("perpetual") .AutoFilterMode = False RowLast = .Cells(Rows.count, "A").End(xlUp).row 'Set rng = .Range("C4:C" & RowLast) Range("A3:J" & RowLast).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range("N1:N6"), Unique:=False 'have also tried Range("A3:J3"), doesn't work. End With ThisWorkbook.Sheets("myperpetual").Range("A5", "J5").PasteSpecial xlPasteValues End Sub 

我试图从数据从A4:J4到一些A16000:J16000的文件复制。 我必须将列C中的值筛选到范围N1:N6中的工作表中指定的范围。
FYI:表头A4是空的,而B4:J4有相关的头文件。
另外,请让我知道,如果我的复制粘贴方法是错误的,或由于某种原因不能按预期的方式工作。

编辑:我也试过在列A,即单元格A3添加标题。 依然不起作用。
我的范围N1:N6是一个数字列表,但我相信错误在那里。 它不指定要应用哪个列filter。

一旦你在With perporig.Sheets("perpetual") ... End With块,所有Range对象和Range.Cells属性引用都以句号(aka或full stop ) 开头 。

 With perporig.Sheets("perpetual") .AutoFilterMode = False RowLast = .Cells(Rows.count, "A").End(xlUp).row 'Set rng = .Range("C4:C" & RowLast) .Range("A3:J" & RowLast).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("N1:N6"), Unique:=False End With 

注意: .Range("C4:C"....Range("N1:N6")以句点开头,表示范围的父级工作表是由With命名的工作表定义的。 End With语句如果没有它,Excel会猜测范围属于哪个工作表,通常这是ActiveSheet属性 。