循环macros复制和粘贴,然后打印

我需要为这个macros创build一个循环:

Sub Site_No() ' Site_No Macro ' Keyboard Shortcut: Ctrl+Shift+J Range("D2").Select Selection.Copy Sheets("Spray Sheet").Select Range("F5:J6").Select ActiveSheet.Paste ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _ IgnorePrintAreas:=False End Sub 

理想情况下,我想循环从D2 – D79。

如果有人能告诉我在哪里放置循环,我会非常感激

奥利

我不会提供你的代码,但会给你一些提示,以实现你的目标。

 Sub Site_No() ' ' Start your loop here from D1 to D79. I suggest to use Cells(Row, Column) notation. Range("D2").Select 'There is no need to select the cell Selection.Copy 'Instead of Copy, you can use .Value Sheets("Spray Sheet").Select 'There is no need to select the Sheet Range("F5:J6").Select 'There is no need to select the cell ActiveSheet.Paste 'You don't need to paste the value. 'End loop here if you need to print only once. ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _ IgnorePrintAreas:=False End Sub 

你可以看到这个问题的例子,使用.Value “复制/粘贴”单元格内容。

通过工作表复制/粘贴循环来合并

这样的东西应该足够了:

 Public Sub TestMe() Dim lngRow As Long For lngRow = 2 To 79 ActiveSheet.Cells(lngRow, "D") = Worksheets("Spray Sheet").Cells(lngRow, "F") Next lngRow End Sub 

使用selectactivate是你应该尽量避免的事情 – 如何避免在Excel VBA中使用Select (在他的代码中使用ActiveSheet的人说)。