将表从一个工作表复制到另一个工作表,同时保持列大小

时间:2016-02-03 10:59:58

标签: excel vba excel-vba

将表从一个工作表复制到另一个工作表,其中要保留原始工作表中的列大小,因为新工作表已经有一个具有较小大小列的表。

Dim i, lastRow

lastRow = Sheets("Closed").Range("A" & Rows.Count).End(xlUp).Row

For i = 1 To lastRow

Sheets("Closed").Cells(i, "A").Copy Destination:=Sheets("Misc.Dashboard").Range("A" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "B").Copy Destination:=Sheets("Misc.Dashboard").Range("B" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "C").Copy Destination:=Sheets("Misc.Dashboard").Range("C" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "D").Copy Destination:=Sheets("Misc.Dashboard").Range("D" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "E").Copy Destination:=Sheets("Misc.Dashboard").Range("E" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "F").Copy Destination:=Sheets("Misc.Dashboard").Range("F" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "G").Copy Destination:=Sheets("Misc.Dashboard").Range("G" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "H").Copy Destination:=Sheets("Misc.Dashboard").Range("H" & Rows.Count).End(xlUp).Offset(1)
Sheets("Closed").Cells(i, "I").Copy Destination:=Sheets("Misc.Dashboard").Range("I" & Rows.Count).End(xlUp).Offset(1)

Next i

First sheet,table to be copied

Second sheet, paste table below the table present

1 个答案:

答案 0 :(得分:1)

因为您似乎正在复制"已关闭"工作表并将其添加到" Misc.Dashboard"的底部。 Sheet,而不是写一个循环,如果数据量增加变得非常慢,尝试一次性复制范围。

Dim lastRow As Long, lastColumn As Long

With ThisWorkbook
    With .Worksheets("Closed")
        lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        lastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
        .Range(.Cells(1, 1), .Cells(lastRow, lastColumn)).Copy
    End With
    With .Worksheets("Misc.Dashboard")
        .Range(.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0), .Cells(.Rows.Count, lastColumn).End(xlUp).Offset(1 + lastRow, 0)).PasteSpecial (xlPasteColumnWidths)
        .Range(.Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0), .Cells(.Rows.Count, lastColumn).End(xlUp).Offset(1 + lastRow, 0)).PasteSpecial (xlPasteValues)
    End With
End With
Application.CutCopyMode = False