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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

urgent

Status
Not open for further replies.

someinput

Programmer
Oct 7, 2002
1
IN
I have an excel template with password protected VBA.If i try to save, it will not allow unless i enter the password.I tried to write a VBA which will create a copy of current excel object and save it in a new location(by throwing save as dialoge control). But stil, since VBA is password protected, it is not possible to do so.So now i am trying to create a new XL application,workbook and copy the
current worksheets to new workbook.I am trying the following code.But it is throwing 'type mismatch'.


Dim xlApp As Excel.Application
Dim xlbook As Excel.Workbook
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Add

Set xlbook = xlApp.ActiveWorkbook
Application.ActiveWorkbook.Sheets(0).Copy before:=xlApp.Workbooks(xlbook).Sheets(0)









 
someinput,

Change this line:

Code:
Application.ActiveWorkbook.Sheets(0).Copy before:=xlApp.Workbooks(
Code:
xlbook
Code:
).Sheets(0)

to this:

Code:
Application.ActiveWorkbook.Sheets(0).Copy before:=xlApp.Workbooks(
Code:
xlbook.Name
Code:
).Sheets(0)

Workbooks() expects an index number or string containing the name of the workbook.

Regards,
Mike
 
Try this as an alternative ......

****************************************************

Dim xlCurrBook As Excel.Workbook, xlCopyBook As Excel.Workbook

Set xlCurrBook = ActiveWorkbook 'declare current workbook
Set xlCopyBook = Workbooks.Add 'declare and add new workbook

xlCurrBook.Sheets(1).Copy before:=xlCopyBook.Sheets(1) 'copy sheets from current to new


****************************************************

If you want to copy multiple sheets maybe you could use :-

xlCurrBook.Sheets(Array(1, 2, 3)).Copy before:=xlCopyBook.Sheets(1)

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top