如何避免在循环vba中selectdynamic行

过去几个月我一直在使用下面的代码。 但我刚刚发现,因为我在工作表中select一个dynamic范围,如果我不在工作表中,我会select方法范围类失败。

下面的代码失败:

With w.Sheets("Sheet1") n = .Cells(Rows.Count, 2).End(xlUp).Row .Range("Y2:AI2").Copy .Cells(n, 25).Select .Range(Selection, Selection.End(xlUp).Offset(1, 0)).PasteSpecial xlPasteFormulas .Application.CutCopyMode = False End With 

以下代码成功:

 With w.Sheets("Sheet1") n = .Cells(Rows.Count, 2).End(xlUp).Row .Range("Y2:AI2").Copy Sheets("Sheet1").Select .Cells(n, 25).Select .Range(Selection, Selection.End(xlUp).Offset(1, 0)).PasteSpecial xlPasteFormulas .Application.CutCopyMode = False End With 

我必须再次select表单。 理想情况下,我想避免select/激活干脆!

任何转向正确的方向将不胜感激!

尝试这个

 With w.Sheets("Sheet1") .Range("Y2:AI2").Copy With .Cells(.Cells(Rows.count, 2).End(xlUp).row, 25) .Parent.Range(.Cells, .End(xlUp).Offset(1, 0)).PasteSpecial xlPasteFormulas End With .Application.CutCopyMode = False End With