将单元格从图纸a复制到图纸B的VBA代码
我有两张纸: SheetA有一份员工编号列表 表B有一张表格,需要填写并打印每个员工的编号(然后填写剩余的vlookup公式) 现在我可以手动复制粘贴每个员工ID,但是有330多名员工,这有点太多了 我想复制表格A中的单元格A2,将其粘贴到表格B中的单元格A2并打印表单,然后转到表格A中的单元格A3复制它,将其粘贴到表格B中的单元格A2,依此类推。。。我想重复这个过程337次 我创建了这个宏,但我不知道如何使它总是选择工作表A中的下一个单元格并重复337次。(或取决于我们在某一时间有多少员工)将单元格从图纸a复制到图纸B的VBA代码,vba,excel,Vba,Excel,我有两张纸: SheetA有一份员工编号列表 表B有一张表格,需要填写并打印每个员工的编号(然后填写剩余的vlookup公式) 现在我可以手动复制粘贴每个员工ID,但是有330多名员工,这有点太多了 我想复制表格A中的单元格A2,将其粘贴到表格B中的单元格A2并打印表单,然后转到表格A中的单元格A3复制它,将其粘贴到表格B中的单元格A2,依此类推。。。我想重复这个过程337次 我创建了这个宏,但我不知道如何使它总是选择工作表A中的下一个单元格并重复337次。(或取决于我们在某一时间有多少员工
您只需在每一行中循环:
Sub Copy_Cell()
Dim r As Long
'Use a "With" block to save having to constantly type "Worksheets("Sheet A")"
'inside the block
With Worksheets("Sheet A")
'Loop through all values in column A, thus saving the trouble of
'hard-coding the last row number to be used
For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
'Just copy the value directly from one worksheet to another, thus
'avoiding copy/paste
Worksheets("Sheet B").Range("A2").Value = .Cells(r, "A").Value
Worksheets("Sheet B").PrintOut Copies:=1, _
Collate:=True, _
IgnorePrintAreas:=False
Next r
End With
End Sub
您只需在每一行中循环:
Sub Copy_Cell()
Dim r As Long
'Use a "With" block to save having to constantly type "Worksheets("Sheet A")"
'inside the block
With Worksheets("Sheet A")
'Loop through all values in column A, thus saving the trouble of
'hard-coding the last row number to be used
For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
'Just copy the value directly from one worksheet to another, thus
'avoiding copy/paste
Worksheets("Sheet B").Range("A2").Value = .Cells(r, "A").Value
Worksheets("Sheet B").PrintOut Copies:=1, _
Collate:=True, _
IgnorePrintAreas:=False
Next r
End With
End Sub
子复制单元()
将行设置为整数
行子复制单元()时执行
将行设置为整数
当循环vba的行查找时执行查找循环vba的行查找您需要将行
的值初始化为1
,否则它将处理单元格A1作为第一个要复制的单元格,而不是A2。非常好的观察结果,谢谢您纠正我。我又发布了。您需要将行
的值初始化为1
,否则它将处理单元格A1作为第一个要复制的单元格,而不是A2。非常好的观察结果,谢谢您纠正我。我又发了一次帖子,非常感谢,伙计,是的,那是一个拼写错误,它们都需要粘贴在同一个单元格中。嘿,非常感谢,伙计,是的,那是一个拼写错误,它们都需要粘贴在同一个单元格中。
Sub Copy_Cell()
Dim r As Long
'Use a "With" block to save having to constantly type "Worksheets("Sheet A")"
'inside the block
With Worksheets("Sheet A")
'Loop through all values in column A, thus saving the trouble of
'hard-coding the last row number to be used
For r = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
'Just copy the value directly from one worksheet to another, thus
'avoiding copy/paste
Worksheets("Sheet B").Range("A2").Value = .Cells(r, "A").Value
Worksheets("Sheet B").PrintOut Copies:=1, _
Collate:=True, _
IgnorePrintAreas:=False
Next r
End With
End Sub
Sub Copy_Cell()
Dim row as Integer
Do While row <= 337
Sheets("Sheet A").Activate
Sheets("Sheet A").Cells(row + 1, 1).Copy
Sheets("Sheet B").Activate
Range("A2").Select
ActiveSheet.Paste
ActiveWindow.SelectedSheets.PrintOut Copies:=1
row = row + 1
Loop
End sub