我需要为这个宏创建一个循环:
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循环播放。
如果有人能告诉我在哪里放置循环,我将非常感激
奥利
答案 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
查看此问题以“复制/粘贴”单元格内容。
答案 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
使用select
和activate
是您应尽量避免的事情 - How to avoid using Select in Excel VBA(在他的代码中使用ActiveSheet
的人说)。