简体   繁体   English

Excel VBA:将多张工作表复制到新工作簿中

[英]Excel VBA: Copying multiple sheets into new workbook

I have an error message of 'Object Required' when I run this sub.运行此子程序时,我收到“需要对象”的错误消息。 I have a version for copying each specific sheet, which works fine, but this sub is for all sheets within the WB ie to copy each one's WholePrintArea and paste it into a new sheet in the new WB.我有一个用于复制每个特定工作表的版本,效果很好,但是这个子程序适用于 WB 内的所有工作表,即复制每个人的 WholePrintArea 并将其粘贴到新 WB 中的新工作表中。 Thanks...谢谢...

Sub NewWBandPasteSpecialALLSheets()

  MyBook = ActiveWorkbook.Name ' Get name of this book
  Workbooks.Add ' Open a new workbook
  NewBook = ActiveWorkbook.Name ' Save name of new book

  Workbooks(MyBook).Activate ' Back to original book

  Dim SH As Worksheet

    For Each SH In MyBook.Worksheets

    SH.Range("WholePrintArea").Copy

    Workbooks(NewBook).Activate

        With SH.Range("A1")
            .PasteSpecial (xlPasteColumnWidths)
            .PasteSpecial (xlFormats)
            .PasteSpecial (xlValues)

        End With

    Next

End Sub

Try do something like this (the problem was that you trying to use MyBook.Worksheets , but MyBook is not a Workbook object, but string , containing workbook name. I've added new varible Set WB = ActiveWorkbook , so you can use WB.Worksheets instead MyBook.Worksheets ):尝试做这样的事情(问题是您尝试使用MyBook.Worksheets ,但MyBook不是Workbook对象,而是包含工作簿名称的string 。我添加了新变量Set WB = ActiveWorkbook ,因此您可以使用WB.Worksheets代替MyBook.Worksheets ):

Sub NewWBandPasteSpecialALLSheets()
   MyBook = ActiveWorkbook.Name ' Get name of this book
   Workbooks.Add ' Open a new workbook
   NewBook = ActiveWorkbook.Name ' Save name of new book

   Workbooks(MyBook).Activate ' Back to original book

   Set WB = ActiveWorkbook

   Dim SH As Worksheet

   For Each SH In WB.Worksheets

       SH.Range("WholePrintArea").Copy

       Workbooks(NewBook).Activate

       With SH.Range("A1")
        .PasteSpecial (xlPasteColumnWidths)
        .PasteSpecial (xlFormats)
        .PasteSpecial (xlValues)

       End With

     Next

End Sub

But your code doesn't do what you want: it doesen't copy something to a new WB.但是您的代码没有做您想做的事:它不会将某些内容复制到新的 WB。 So, the code below do it for you:所以,下面的代码为你做:

Sub NewWBandPasteSpecialALLSheets()
   Dim wb As Workbook
   Dim wbNew As Workbook
   Dim sh As Worksheet
   Dim shNew As Worksheet

   Set wb = ThisWorkbook
   Workbooks.Add ' Open a new workbook
   Set wbNew = ActiveWorkbook

   On Error Resume Next

   For Each sh In wb.Worksheets
      sh.Range("WholePrintArea").Copy

      'add new sheet into new workbook with the same name
      With wbNew.Worksheets

          Set shNew = Nothing
          Set shNew = .Item(sh.Name)

          If shNew Is Nothing Then
              .Add After:=.Item(.Count)
              .Item(.Count).Name = sh.Name
              Set shNew = .Item(.Count)
          End If
      End With

      With shNew.Range("A1")
          .PasteSpecial (xlPasteColumnWidths)
          .PasteSpecial (xlFormats)
          .PasteSpecial (xlValues)
      End With
   Next
End Sub

Rethink your approach.重新考虑你的方法。 Why would you copy only part of the sheet?为什么只复制表格的一部分? You are referring to a named range "WholePrintArea" which doesn't exist.您指的是一个不存在的命名范围“WholePrintArea”。 Also you should never use activate, select, copy or paste in your script.你也不应该在你的脚本中使用激活、选择、复制或粘贴。 These make the "script" vulnerable to user actions and other simultaneous executions.这些使“脚本”容易受到用户操作和其他同时执行的影响。 In worst case scenario data ends up in wrong hands.在最坏的情况下,数据最终会落入坏人手中。

This worked for me (I added an "if sheet visible" because in my case I wanted to skip hidden sheets)这对我有用(我添加了一个“如果工作表可见”,因为在我的情况下我想跳过隐藏的工作表)

   Sub Create_new_file()

Application.DisplayAlerts = False

Dim wb As Workbook
Dim wbNew As Workbook
Dim sh As Worksheet
Dim shNew As Worksheet
Dim pname, parea As String


Set wb = ThisWorkbook
Workbooks.Add
Set wbNew = ActiveWorkbook

For Each sh In wb.Worksheets

    pname = sh.Name


    If sh.Visible = True Then

    sh.Copy After:=wbNew.Sheets(Sheets.Count)

    wbNew.Sheets(Sheets.Count).Cells.ClearContents
    wbNew.Sheets(Sheets.Count).Cells.ClearFormats
    wb.Sheets(sh.Name).Activate
    Range(sh.PageSetup.PrintArea).Select
    Selection.Copy

    wbNew.Sheets(pname).Activate
    Range("A1").Select

    With Selection

        .PasteSpecial (xlValues)
        .PasteSpecial (xlFormats)
        .PasteSpecial (xlPasteColumnWidths)

    End With

    ActiveSheet.Name = pname

    End If


Next

wbNew.Sheets("Hoja1").Delete

Application.DisplayAlerts = True

End Sub

Since you are copying all worksheet, how about:由于您要复制所有工作表,如何:

Copy & Paste (X) SaveAS (O)复制和粘贴 (X) SaveAS (O)

Sub Export()

Application.DisplayAlerts = False
On Error Resume Next

Dim NewWB As String

NewWB = Sheets("Control").Range("B42")

ActiveWorkbook.SaveAs Filename:=NewWB, FileFormat:=xlWorkbookNormal
ActiveWorkbook.Sheets("Control").Delete

End Sub

I had a worksheet "Control" handling all variant, you may change it yourself我有一个处理所有变体的工作表“控制”,您可以自己更改它

On the other hand, if you really wish to use COPY & PASTE, you could use ARRAY另一方面,如果你真的想使用 COPY & PASTE,你可以使用 ARRAY

Workbooks.Add
ActiveWorkbook.SaveAs Filename:=FolderPath & ExcelName & ".xlsx", FileFormat:=xlNormal

Workbooks(ExcelOrigin).Activate
    Sheets(Array("for coversheet", "Pivot", "CCA", "FRR", "CRS", "GSA", "Inv Summary", "UploadtoJDE", "Comat")).Copy Before:=Workbooks(ExcelName).Sheets(1)
Sheets("Sheet1").Delete

Remember to Dim (FolderPath,ExcelName,ExcelOrigin) as String As equal them to your file name & file path [ i can't type in those here because of error ]请记住将Dim (FolderPath,ExcelName,ExcelOrigin) as String它们与您的文件名和文件路径相同[由于错误,我无法在此处输入]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA将工作表复制到新工作簿并在Outlook中通过电子邮件发送新工作簿 - Excel VBA Copying Sheets to a New Workbook and emailing the New Workbook in Outlook Excel VBA复制工作表格式不同的工作表 - Excel vba copying sheets different workbook format VBA通过自动创建新工作簿将选定的多个工作表数据范围复制到工作簿 - VBA Copying Multiple sheets selected Data ranges to a workbook by creating a new workbook automatically 将多张工作表复制到新工作簿中(用于分发) - Copying multiple sheets into a new workbook (for distribution) Excel VBA:在新工作簿中保存多个工作表 - Excel VBA: Saving several sheets in a new workbook VBA代码问题,用于从Excel工作簿中的多个工作表复制和粘贴相同的列 - VBA code issue for copying and pasting same columns from multiple sheets in excel workbook 使用vba创建新工作簿,复制多张纸中的2张时出错 - Error when creating new workbook with vba, copying 2 of many sheets Excel从多张工作表复制不起作用vba - Excel copying from multiple sheets not working vba 复制行,并在多个工作表上插入带有粘贴信息的多个新行Excel VBA - Copying a row, inserting multiple new rows with the pasted info on multiple sheets Excel VBA VBA将Excel工作表复制到另一个工作簿中的新工作表 - VBA copying an Excel worksheet to a new worksheet in another workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM