循环复制和宏的宏粘贴然后打印

时间:2017-11-22 12:57:46

标签: excel vba excel-vba loops

我需要为这个宏创建一个循环:

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循环播放。

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

奥利

2 个答案:

答案 0 :(得分:1)

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

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查看此问题以“复制/粘贴”单元格内容。

  

Copy/Paste Loop through worksheets to consolidate

答案 1 :(得分:1)

这样的事情应该足够了:

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是您应尽量避免的事情 - How to avoid using Select in Excel VBA(在他的代码中使用ActiveSheet的人说)。