在应用filter粘贴任何东西后粘贴在新的表格中

我试图运行一个macros应该得到最后一个活跃的行,复制所有数据到一个新的表,应用filter(K行> 15,9号),复制并粘贴结果在一个新的工作表。

但是,在使用filter之后,没有任何东西粘贴在新的纸上。 任何想法为什么?

谢谢!

Sub Macro1() 'Select and paste all data, couldn't work on a "last active line" in here.. Cells.Select Selection.Copy Sheets("Plan2").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Range("A1:O1").Select Application.CutCopyMode = False 'Aplying the filter Selection.AutoFilter ActiveSheet.Range("$A$1:$O$1056").AutoFilter Field:=11, Criteria1:=">15,9" _ , Operator:=xlAnd 'Here I'm trying to past the filtered data in the new sheet, but the result appears in blank Cells.Select Selection.Copy Sheets("Plan3").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False 'Here i came back and turned the autofilter off, but it was useless Sheets("Plan2").Select Application.CutCopyMode = False ActiveSheet.Range("$A$1:$O$1056").AutoFilter Field:=11 End Sub 

代码中有几个问题:

  1. 使用Cells可能非常棘手,特别是当你没有很好地定义它们的时候
  2. 使用Select从来不是一个好的做法。 只要坚持直接与对象(表格,范围,工作簿等)工作。
  3. 我不知道为什么你需要将整个数据集复制到一张新纸上,然后过滤它以复制到第三张纸上。 可以只过滤原始数据集并复制到最终表单。 我没有为此调整代码,因为可能有一个原因需要这样做,但是您知道,只需处理原始数据,而不需要中间步骤复制到另一个表单进行过滤。
  4. 即使逗号是小数点分隔符,您可能也需要过滤15.9而不是15.9 。 (这可能不是真的,但是我join了它(如果我没有在Excel中使用欧洲系统的经验)。另请参阅上面的David Zemens注释。

在下面的代码中,我已经限定了所有的表单和范围,find了最后一行,并提供了一些我做了一些假设的注释。 修改它以适应您的确切结构,并让我知道它是否工作。

 Sub Macro1() 'Select and paste all data, couldn't work on a "last active line" in here.. Dim wsCopy As Worksheet, wsPlan2 As Worksheet, wsPlan3 As Worksheet Set wsCopy = Sheets("mySheet") ' replace with correct sheet name Set wsPlan2 = Sheets("Plan2") Set wsPlan3 = Sheets("Plan3") 'this will copy only cells with data in (*note -> this could copy more than that, but I will not go into it, for now, it's sufficient to use this) With wsCopy 'find last row Dim lRow As Long lRow = .Range("A" & .Rows.Count).End(xlUp).Row .Range("A1:O" & lRow).Copy 'assume the data goes to column O, change if need be End With With wsPlan2 'paste to sheet Plan 2 .Range("A1").PasteSpecial xlPasteValues 'find last row lRow = .Range("A" & .Rows.Count).End(xlUp).Row With .Range("A1:O" & lRow) 'Aplying the filter .AutoFilter 11, ">15,9" 'you may need to use 15.9 here if you indeed mean 15 and 9/10ths. even if the comma separator is what you use to show decimals .Copy End With wsPlan3.Range("A1").PasteSpecial xlPasteValues 'change range reference if you need it .AutoFilterMode = False End With End Sub