如何将工作表1复制到工作表2

时间:2020-04-19 15:33:59

标签: excel vba

我有两张纸,sheet1包含所有员工一周花名册的所有信息,我需要基于Employee ID作为主键将信息从sheet1复制到sheet2 在sheet2中,我仅显示员工ID和姓名。 例如,员工21253,MON上的班次记录应在第1天的班次中显示,TUE应该在第2天的班次中显示,WED应该在第3天的班次中显示,等等。SHift2列应保留为空白(跳过此列) 工作表1 enter image description here

Sheet2: enter image description here

我的代码写在下面:它确实复制了所有信息,但没有复制工作表2中显示的格式。

Sub transferSheet2()
Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long
Dim myname As String
lastrow1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow1
myname = Sheets("Sheet1").Cells(i, "A").Value

Sheets("Sheet2").Activate
lastrow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

For j = 2 To lastrow2

If Sheets("Sheet2").Cells(j, "A").Value = myname Then
Sheets("Sheet1").Activate
'copy in sheet1 from column B to I
Sheets("Sheet1").Range(Cells(i, "B"), Cells(i, "I")).Copy
Sheets("Sheet2").Activate
'paste in sheet1 from column B to I
Sheets("Sheet2").Range(Cells(j, "B"), Cells(j, "I")).Select
ActiveSheet.Paste
End If

Next j
Next i
'insert new column
'Call insert_column_every_other

'insert headers
'Call Copy_Header
'Autofit columns

ThisWorkbook.Worksheets("Sheet2").Cells.EntireColumn.AutoFit
End Sub

1 个答案:

答案 0 :(得分:0)

更改ActiveSheet.Paste

收件人:-

ActiveSheet.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
             Operation:= xlNone, SkipBlanks:=False, Transpose:=False

xlPasteValuesAndNumberFormats包含格式。