简体   繁体   中英

How to copy multiple sheets to a new workbook?

I am trying to create a Save As function for a report.

The Master sheet has 25 tabs. I am looking to copy 23 of the 25 to a daily Save As to a specific folder.

I have an Error Message warning box blocking users from saving the file so they are unable to mess with it.

Sub SaveMain()

Application.EnableEvents = False
'Stop ThisWorkbook VBA currently blocking the user being able to Save

Dim FlName As String
Dim FilePath As String
Dim NewWkBk As Workbook
Dim FileDate As String

    FlName = " September Reporting" & InputBox("Enter Yesterday's Date DD/MM/YYYY:", "Creating New File...") & ".xlsb"
    FilePath = "Z:\Call Agent Brief\Reporting\September Reporting\Reports"

    Set NewWkBk = Workbooks.Add
    Windows("September Reports Calculator - MASTER COPY.xlsb").Activate
    Sheets(Array("Admin Tab", "Home Tab", "Dashboard", "Drop Down Values", _
        "Reports Home", "Deployments", "Daily Summary", "Daily Breakdown", _
        "Monthly Summary", "Monthly Breakdown - Title Page", "Monthly Breakdown", _
        "Monthly Rolling 12 Months", "Monthly Cancellations", "Non-Deployments", _
        "Non-Deployments Summary", "Non-Deployments Breakdown", "FNOL", "FNOL Summary", _
        "FNOL Breakdown", "FNOL Deployments by User", "FNOL Deployments by Team", _
        "FNOL Deployments by Insurer", "FNOL Non-Deployed Opportunities")).Copy After:=Workbooks(NewWkBk)
    NewWkBk.SaveAs FilePath & "\" & FlName, FileFormat:=xlsb


Application.EnableEvents = True

End Sub

When running the macro the form for the date entry correctly loads, however a runtime error message appears.

Run-time error '13': Type mismatch

The debug highlights the long Sheets copy line. Even when I limit it to copying one tab it comes back with the same issue.

I cannot Paste Values to a new sheet as there is a lot of formatting in the Master sheet designed to make this user friendly. As far as I can tell the only way is to make a copy of the entire sheet.

You are going to have issues I think with what you are naming the file, starting with a space, using "/" in the name. Once you fix that I think this will work:

Sub SaveMain()

Application.EnableEvents = False
'Stop ThisWorkbook VBA currently blocking the user being able to Save

Dim FlName As String
Dim FilePath As String
Dim NewWkBk As Workbook
Dim FileDate As String

    FlName = "September Reporting" & InputBox("filename ", "Creating New   File...") & ".xlsb"
    FilePath = "C:\users\adm123\documents\xlworking"
    Sheets(Array("Admin Tab", "Home Tab", "Dashboard", "Drop Down Values", _
        "Reports Home", "Deployments", "Daily Summary", "Daily Breakdown", _

        "Monthly Summary", "Monthly Breakdown - Title Page", "Monthly Breakdown", _

        "Monthly Rolling 12 Months", "Monthly Cancellations", "Non-
Deployments", _

        "Non-Deployments Summary", "Non-Deployments 
Breakdown", "FNOL", "FNOL Summary", _

        "FNOL Breakdown", "FNOL Deployments by User", "FNOL Deployments by 
Team", _


            "FNOL Deployments by Insurer", "FNOL Non-Deployed 
Opportunities")).Copy

    newfilename = FilePath & "\" & FlName

    With ActiveWorkbook
    .SaveAs newfilename, FileFormat:=50
    .Close 0

    End With


Application.EnableEvents = True


End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM