Macro to open template

aa2000

Board Regular
Joined
Aug 3, 2011
Messages
87
Hi again (Last question, I promise....maybe)

I am currently have a macro which opens a new workbook and puts in sheets depending on certain conditions, using the code below:

Code:
Application.SheetsInNewWorkbook = 1
Set Newbook = Workbooks.Add

With Newbook
                    .SaveAs Filename:=ThisWorkbook.Path & "\" & ActiveWorkbook.Sheets.Item(10).name & "_to_" & ActiveWorkbook.Sheets.Item(1).name & ".xls"
                    End With
'^line to save book

How can this be modified to open a specific template rather than the newbook?

Additionally the template contains some macros. How can i get the above macro to run these once the file has been saved.

The macro to run is just:
Code:
Sub Compare ()
Call comp1
Call comp2
Call comp3
End Sub

Thanks!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I'm not 100% sure of what exactly you're asking for but never the less here are some thoughts.


  1. Open a specific "template" by simply provide the filename and setting the object Newbook to point to that template instead of a new workbook
  2. You can e.g. call the macros using your Newbook object.
In my example I have two workbooks Book1.xls and Book2.xls. In Book1 there's a macro Macro1 and in Book2 there's another macro Macro2.

I simply call Macro1 in Book1.xls through the object Newbook as:
Code:
Dim NewBook As Workbook

Sub Macro2()
    Set Newbook = Workbooks(1)
    Application.Run (Newbook.Name & "!Macro1")
End Sub
You'll have to replace the opening procedure to anyone suitable to your needs e.g. by passing a filepath etc.

BR,
Perco
 
Upvote 0
Thank you Perco, that was what I was asking, and I'll try to run this to see if it works.

Cheers
 
Upvote 0
Ok so I have set NewBook to point to the particular template like so:

Code:
Set NewBook = Workbooks(ThisWorkbook.Path & "\" & "ImportTemplate.xlt").Add
However I get Runtime error 9 "Subscript out of range". Whats wrong with this code now?

If I try it with no "" around the .xlt file I get and Object required error instead.

Cheers
 
Upvote 0
Use this code:

Code:
    Dim NewBook As Workbook
    Dim fileName As String
    
        fileName = ThisWorkbook.Path & "\" & "ImportTemplate.xlt"
        Set NewBook = Application.Workbooks.Add(fileName)
Ok so I have set NewBook to point to the particular template like so:

Code:
Set NewBook = Workbooks(ThisWorkbook.Path & "\" & "ImportTemplate.xlt").Add
However I get Runtime error 9 "Subscript out of range". Whats wrong with this code now?

If I try it with no "" around the .xlt file I get and Object required error instead.

Cheers
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top