简体   繁体   中英

Excel: Copy workbook to new workbook

So prior to asking this I searched and found something that was similar to what I was looking to do here.

Basically I have workbook AlphaMaster. This workbook is a template that I want to use to create new workbooks from weekly.

In this workbook there are sheets named: Monday-Saturday and additional sheets with a corresponding date for Mon, Tues, ect.

I have created a Form that loads on open of the workbook. What I want is when I click form run it will:

  • Run Code saving template as new workbook
  • Rename workbook based of input from userform1
  • Rename the workbooks with proper weekday

    Workbook is named for a week end date dates of 6 sheets would renamed after this(example week ending 5th of Jan.) is put into user form as:

WeekEnd: Jan-5-2014 Dates Mon:Dec.30 Tues:Dec.31 Weds:Jan.1 Thurs:Jan.2 Fri:Jan.3 Sat:Jan.4

Than click command. so far this is what I have:

 Private Sub CommandButton1_Click()
Dim thisWb As Workbook, wbTemp As Workbook
Dim ws As Worksheet

On Error GoTo dummkopf

Application.DisplayAlerts = False

Set thisWb = ThisWorkbook
Set wbTemp = Workbooks.Add

On Error Resume Next
For Each ws In wbTemp.Worksheets
    ws.Delete
Next
On Error GoTo 0

For Each ws In thisWb.Sheets
    ws.Copy After:=wbTemp.Sheets(1)
Next

wbTemp.Sheets(1).Delete
wbTemp.SaveAs "blahblahblah\New.xlsx"

new.xlsx i want to be filled in from form

Vorfahren:
Application.DisplayAlerts = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Vorfahren
End Sub

Complications:

Currently while this does work I cant change the name of the document its named what I name it in the .saveAs area. I'm thinking I need to create an alternate function to handle this. Second, when it finishes my sheets are displayed in the reverse order of the template.

Some guidance/suggestions on where to go from here would be greatly appreciated!

A few issues here:

You cannot delete all Worksheets in a Workbook.

WorksheetDeleteError

You should copy the sheet to the end to retain order (if the worksheets in source workbook is sorted):

For Each ws In thisWb.Sheets
    ws.Copy After:=wbTemp.Sheets(wbTemp.Sheets.Count)
    wbTemp.Sheets(wbTemp.Sheets.Count).Name = "NewSheetName" ' <-- Rename the copied sheet here
Next

If your source Worksheets does not have names "Sheet#" then delete the default sheets afterwards.

Application.DisplayAlerts = False
For Each ws In wbTemp.Sheets
    If Instr(1, ws.Name, "Sheet", vbTextCompare) > 0 Then ws.Delete
Next
Application.DisplayAlerts = True

For SaveAs, refer to Workbook.SaveAs Method (Excel) .

I use this in my application and works good

Set bFso = CreateObject("Scripting.FileSystemObject")
bFso.CopyFile ThisWorkbook.FullName, destinationFile, True     

Once it's copied you can then open it in new Excel Object and do what ever you want with it.

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