【问题标题】:How to copy sheet1 to sheet2如何将sheet1复制到sheet2
【发布时间】:2020-08-02 01:02:39
【问题描述】:

我有两张表,sheet1 包含所有员工一周的花名册的所有信息,我需要根据员工 ID 作为主键将信息从 sheet1 复制到 sheet2 在 sheet2 中,我只显示了员工 ID 和姓名。 比如员工21253,MON的班次记录应该显示在day1-shift 1中,TUE应该显示在day2-shift1中,WED应该显示在day3-shift1中等等。SHIft2栏应该保持空白(跳过此栏) 表 1

表 2:

我的代码写在下面:它确实复制了所有信息,但不是以表 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 行复制和粘贴(包括格式),要么您可以使用当前方法并粘贴两次(一次用于值,一次用于格式)xlPasteSpecial
  • 看看这个问题和答案:stackoverflow.com/q/30575923/4961700
  • edit您的问题,因为您似乎发布了真实的个人数据

标签: excel vba


【解决方案1】:

更改ActiveSheet.Paste

收件人:-

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

xlPasteValuesAndNumberFormats 包含格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2022-01-19
    相关资源
    最近更新 更多