从一张纸粘贴到另一张到第一个空白行

我已经看到一些例子,但他们一直在使用.Select和.Activate。 我正试图学习如何不再使用这些,因为大家都说你应该尽量远离它们。

我想取一排,然后将其复制到另一张纸上的第一个空白行。 我很近,但它不工作。

UsdRws = Range("A" & Rows.Count).End(xlUp).Row With Sheets("Totals by Department") .Range("A1:Z" & UsdRws).autofilter Field:=1, Criteria1:="1450" .Range("A2:Z" & UsdRws).SpecialCells(xlCellTypeVisible).EntireRow.COPY End With Set NextRow = Range("A" & Sheets(2).UsedRange.Rows.Count + 1) Sheets(2).NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Set NextRow = Nothing 

第一部分完美复制,我真的只需要粘贴在另一张纸上的帮助。 我也会采取其他的build议来清理代码。 就像我说的,我正在努力学习写得更好。 第二部分是杂乱的,因为我一直在添加和编辑它,但现在我迷路了。

您的“NextRow”对象是一个Range对象,但是您将其称为是Sheets(2)的方法或属性。

尝试删除表格(2)。 并从下一行开始。

 Set NextRow = Sheets(2).Range("A" & Sheets(2).UsedRange.Rows.Count + 1) NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False 
 ' UsdRws is equal the last used row on whichever sheet is active at the moment that this code runs UsdRws = Range("A" & Rows.Count).End(xlUp).Row ' this code properly references ranges on a specific worksheet, regardless of which worksheet is active With Sheets("Totals by Department") .Range("A1:Z" & UsdRws).autofilter Field:=1, Criteria1:="1450" .Range("A2:Z" & UsdRws).SpecialCells(xlCellTypeVisible).EntireRow.COPY End With ' NextRow is reference to a cell on whichever sheet is active at the moment that this code runs ' but the row referenced is same as the first emply cell on Sheets(2) Set NextRow = Range("A" & Sheets(2).UsedRange.Rows.Count + 1) ' NextRow is already a range .... so it should be NextRow.PasteSpecial ...... Sheets(2).NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Set NextRow = Nothing 

这可能是你想要的

 With Sheets("Totals by Department") UsdRws = .Range("A" & .Rows.Count).End(xlUp).Row .Range("A1:Z" & UsdRws).autofilter Field:=1, Criteria1:="1450" .Range("A2:Z" & UsdRws).SpecialCells(xlCellTypeVisible).EntireRow.COPY End With Set NextRow = Sheets(2).Range("A" & Sheets(2).UsedRange.Rows.Count + 1) NextRow.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Set NextRow = Nothing