将整个工作表复制到Excel 2010中的新工作表

我发现类似的问题涉及在一个工作簿中复制整个工作表并将其粘贴到另一个工作簿,但我感兴趣的是简单地复制整个工作表并将其粘贴到新工作表 - 在同一工作簿中 .

我正在将2003 .xls文件转换为2010 .xlsm,并且用于在工作表之间复制和粘贴的旧方法不会粘贴正确的行高 . 我最初的解决方法是遍历每一行并从我正在复制的工作表中获取行高,然后循环并在我粘贴的工作表中插入行高的那些值,但这种方法的问题在于sheet包含生成新行的按钮,这些行更改行编号,并且工作表的格式使得所有行不能只是一个宽度 .

我真正希望能够做的只是复制整个工作表并粘贴它 . 以下是2003版的代码:

ThisWorkbook.Worksheets("Master").Cells.Copy
newWorksheet.Paste

我很惊讶转换为.xlsm导致现在破坏 . 任何建议或想法都会很棒 .

回答(6)

2 years ago

只需运行如下所示的精确副本就可以将副本放入最后一页

Sub Test()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Master")
ws1.Copy ThisWorkbook.Sheets(Sheets.Count)
End Sub

2 years ago

ThisWorkbook.Worksheets("Master").Sheet1.Cells.Copy _
    Destination:=newWorksheet.Cells

以上将复制细胞 . 如果你真的想复制整张表,那么我会选择@brettdj's answer .

2 years ago

' Assume that the code name the worksheet is Sheet1

' Copy the sheet using code name and put in the end.
' Note: Using the code name lets the user rename the worksheet without breaking the VBA code
Sheet1.Copy After:=Sheets(Sheets.Count)

' Rename the copied sheet keeping the same name and appending a string " copied"
ActiveSheet.Name = Sheet1.Name & " copied"

2 years ago

我真的很喜欢@brettdj 's code, but then I found that when I added additional code to edit the copy, it overwrote my original sheet instead. I'已经调整了他的答案,以便进一步的代码指向 ws1 将影响新的工作表而不是原始工作表 .

Sub Test()
    Dim ws1 as Worksheet
    ThisWorkbook.Worksheets("Master").Copy
    Set ws1 = ThisWorkbook.Worksheets("Master (2)")
End Sub

2 years ago

'Make the excel file that runs the software the active workbook
ThisWorkbook.Activate

'The first sheet used as a temporary place to hold the data 
ThisWorkbook.Worksheets(1).Cells.Copy

'Create a new Excel workbook
Dim NewCaseFile As Workbook
Dim strFileName As String

Set NewCaseFile = Workbooks.Add
With NewCaseFile
    Sheets(1).Select
    Cells(1, 1).Select
End With

ActiveSheet.Paste

2 years ago

如果有人像我一样拥有一个包含默认数量的可见定价表的估算工作簿,一个摘要和大量隐藏的和“受保护的”工作表,其中包含敏感数据,但可能需要创建其他可见的工作表才能获得正确的工作表 . 价格,我有上述响应的变体,基于受保护的隐藏“主”创建所述可见工作表 . 我使用了@ / jean-fran%c3%a7ois-corbett和@ thanos-a与简单VBA组合提供的代码,如下所示 .

Sub sbInsertWorksheetAfter()

'This adds a new visible worksheet after the last visible worksheet

    ThisWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)

    'This copies the content of the HIDDEN "Master" worksheet to the new VISIBLE ActiveSheet just created

    ThisWorkbook.Sheets("Master").Cells.Copy _
        Destination:=ActiveSheet.Cells

    'This gives the the new ActiveSheet a default name

    With ActiveSheet
        .Name = Sheet12.Name & " copied"
    End With

    'This changes the name of the ActiveSheet to the user's preference

    Dim sheetname As String

    With ActiveSheet
        sheetname = InputBox("Enter name of this Worksheet")
        .Name = sheetname
    End With

结束子