Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a copy of an activeworksheet to a new workbook

Status
Not open for further replies.

PorscheGT2

Programmer
Jan 20, 2004
23
US
Ok, here's the code I did last night:

Private Sub Create_Copy_Click()

Unload UserOptForm
SrvArea = Range("d5")
svname = SrvArea & " - BOBCopy.xls"
Workbooks.Add.SaveAs FileName:=svname
Windows("The 2004 Book of Business Report.xls").Activate
ActiveSheet.Select
ActiveSheet.Copy Before:=Workbooks(svname).Sheets(1)
Windows(svname).Activate
User_Options.Visible = False
About_Button_SA.Visible = False
Exit_Bob_SA.Visible = False

End Sub

This code works in Excel 2000 also saved as 97, but when I ran the macro this morning on Excel 97 it gave me an error. Is there a better way to do this? TIA!
 
Windows("The 2004 Book of Business Report.xls").activesheet.copy

will create a new workbook with one sheet - your copied sheet - then just name it...

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Hello Geoff,

Thanks for your reply. For some reason, when it executes the line:

ActiveSheet.Copy Before:=Workbooks(svname).Sheets(1)

it gives me the error "Copy Method of Worksheet Class Failed."

Though when I execute it as a macro in a module, it works just fine. My co worker told me it may have something to do with the fact that it's not global. Here's the whole subroutine for your reference:

Private Sub Create_Copy_Click()

Unload UserOptForm
Application.ScreenUpdating = False
srvarea = Range("d5")
svname = srvarea & " - BOBCopy.xls"
Workbooks.Add.SaveAs FileName:=svname
Windows("Copy of The 2004 Book of Business Report v1.0b.xls").Activate
Worksheets(ActiveSheet.Name).Select
Worksheets(ActiveSheet.Name).Copy Before:=Workbooks(svname).Sheets(1)
Windows(svname).Activate
ActiveSheet.Shapes("commandbutton1").Visible = False
ActiveSheet.Shapes("commandbutton2").Visible = False
Workbooks(svname).Save


End Sub

I am executing this from a commandbutton by the way. It works ok in Excel 2000, though it's giving me problems in 97. Do you know what's wrong or missing in my code? TIA!



 
What I'm suggesting is that you don't need to create a new workbook specifically. this line will create a new one on its own:

Windows("The 2004 Book of Business Report.xls").activesheet.copy

And this line will name it:
Activeworkbook.SaveAs FileName:=svname

easier - yes ?

Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Just a though, if the Create_Copy control is in the UserOptForm form, you may consider to place this line "Unload UserOptForm" nearer the End Sub.


Hope This Help
PH.
 
Geoff,

I am have a similar situation. I am using:

Workbooks.Add
newbook = ActiveWorkbook.Name
'save sheets from Prop1Compute to newbook
Workbooks("Prop1Compute.xls") _
.Sheets(Array("MAIN-PT", "WYND-PT", "SATL-PT", "NOVA-PT", _
"InvoicePage1", "InvoicePage2", "InvoicePage3")).Copy _
Before:=Workbooks(newbook).Sheets(1)
'save to Prop1Months directory.
ActiveWorkbook.SaveAs _
Filename:="C:\MSOffice\Prop1Months\" & "Prop1" & _ ReportMonth & ".xls"

But the line: Windows("NewBook.xls").ActiveSheet.Copy
generates "Subscript out of range". Changing .Copy to
.Add generates the same error.

Bill
 
I don't think you can do Activesheet.copy - I think you have to specify either the name or the index

This should create a new workbook with the sheets in:

Workbooks("Prop1Compute.xls").Sheets(Array("MAIN-PT", "WYND-PT", "SATL-PT", "NOVA-PT", _
"InvoicePage1", "InvoicePage2", "InvoicePage3")).Copy

and this would save it:
ActiveWorkbook.SaveAs _
Filename:="C:\MSOffice\Prop1Months\" & "Prop1" & _ ReportMonth & ".xls"


Rgds, Geoff
[blue]Si hoc signum legere potes, operis boni in rebus Latinus alacribus et fructuosis potiri potes![/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top