![]() |
|
#1
|
|||
|
|||
![]()
Hi. I can create a word template and when I click on it, it opens a new word document based on the template. i do not write to the template and do not have to do a save as to keep the template as it is. I would like to do this for Excel so the template stays as a unique file.
Can this be done? xlsxx |
#2
|
|||
|
|||
![]()
Yes it can be done. Here is code that creates new sheets that conform to a TEMPLATE sheet located in the same workbook.
Code:
Sub MoreAndMoreSheets() Dim ListSh As Worksheet, BaseSh As Worksheet Dim NewSh As Worksheet Dim ListOfNames As Range, LRow As Long, Cell As Range With ThisWorkbook Set ListSh = .Sheets("List") '--Qualify our sheets. Set BaseSh = .Sheets("Template") End With LRow = ListSh.Cells(Rows.Count, "A").End(xlUp).Row '--Get last row of list. Set ListOfNames = ListSh.Range("A2:A" & LRow) '--Qualify our list. With Application .ScreenUpdating = False '--Turn off flicker. .Calculation = xlCalculationManual '--Turn off calculations. End With For Each Cell In ListOfNames '--For every name in list... BaseSh.Copy After:=Sheets(Sheets.Count) '--Copy Template sheet. Set NewSh = ActiveSheet '--Let's name it NewSh. With NewSh On Error GoTo messageinfo '--In case of errors. .Name = Cell.Value '--Set the sheet's name to that of our current name in list. GoTo LetUsContinue '--Skip to the LetUsContinue block. LetUsContinue: On Error GoTo 0 '--Turn off error handling. .Range("C1") = Cell.Value '--Change C1 to the name of current sheet. .Calculate '--Calculate page. .Cells.Copy '--Change all cells... .Cells.PasteSpecial xlPasteValues '--... to values. End With Next Cell With Application .Calculation = xlCalculationAutomatic '--Return to automatic calculation. .ScreenUpdating = True '--Return to proper state. End With Exit Sub messageinfo: '--In case of duplicate names... MsgBox ("Duplicate name exists") Err.Clear On Error GoTo 0 End Sub When you run the macro it auto creates all of the sheets in the list based on the TEMPLATE sheet. See attached. |
#3
|
|||
|
|||
![]()
In Word, documents are permanently connected to the template they were created from, and not everything in the template gets transferred to the document.
Templates behave slightly differently in Excel. The entire template becomes a new workbook with no connection back to the template. You create Excel templates in exactly the same way as you create a Word template, simply save as a template file (.xltx or .xltm). |
#4
|
|||
|
|||
![]()
"I would like to do this for Excel so the template stays as a unique file."
Based the OP's request I provided an Excel answer. Thank you for your input. |
#5
|
|||
|
|||
![]()
Problem with Italophile’s reply is, no matter how hard I try, it will not save it in my Excel folder. It insists on saving it to C:\Users\User\Documents\Custom Office Templates. I have to drag it kicking and screaming to my folder.
When I create a sheet and click save, it defaults to saving it as the template. In word it defaults to a save as. Even though I launch the template from my Excel folder, the default location is back in C:\Users\User\Documents\Custom Office Templates. I would have to create a templte.xls file with the name template and force myself to do a save as. As for Logit’s solution, although it looks professional, I haven’t got a clue how to do this. I am well proficient in basic and VB.Net though. I have used codeing in Excel. |
#6
|
|||
|
|||
![]()
Here is a description (long I know but bear with me) to assist you with this macro.
Paste the macro into a regular module. If you are unfamiliar with the process ... there are instructions on the internet you can follow. You can find simple instructions on the followng webpage, scroll down a bit until you find Copy Excel VBA Code to a Regular Module and the web page is : https://www.contextures.com/xlvba01.html Secondly, I am presuming you probably don't have the DEVELOPER TAB showing on the menu bar at the top of the Excel workbook. You need to activate it as a means of accessing macros, creating/deleting/editing macros or adding a command button or other controls to your worksheet, form, etc. Go to the Excel Office Button, the top left most corner of the workbook. Click on it to access the various settings. When open click on EXCEL SETTINGS. Kinda in the top middle of the small window find : Show Developer Tab In The Ribbon. Make certain this is checked as active. Now close the small window and you should see the DEVELOPER tab in the menu bar. You can open an empty workbook and play around with the menu selections to see what they do. There are also plenty of instructions on the internet related to the DEVELOPER TAB. What you will want to do is access the developer tab, click INSERT, then select the small button icon located in the left corner of the small window that opens. Click on that small button, move your cursor to the sheet and location where you want the button and left click. You should have a command button on your sheet now. After placing the button on the sheet another small window will open with a list of all the macros in your workbook. In this scenario you'll want to choose the macro name MoreAndMoreSheets, then click OK. The button is now connected to that macro and any time you click the button the macro will run. |
#7
|
|||
|
|||
![]()
This is where I have got to. Attached. I vaugly remember something about changing the file from xls to xlsm or something.
|
#8
|
|||
|
|||
![]()
Ok, so the button control remains the upper left corner.
|
#9
|
|||
|
|||
![]()
This is what I have done. I try the button on the form and get a MsgBox Run time error 9, Subscript out of range. This is on the tab MACRO SHEET.
I want duplicte sheets of Sample sheet. |
#10
|
|||
|
|||
![]() Code:
Option Explicit Sub MakeCopy() Dim i As Integer Application.CopyObjectsWithCells = False For i = 1 To 2 Sheets("Sample Sheet").Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = "Sample Sheet" & Sheets.Count + 1 Next i Application.CopyObjectsWithCells = True 'reset End Sub |
#11
|
|||
|
|||
![]()
I click on the macro icon at the top with a red scroll. The create button is greyed out so I can not create another routeen. I right click on the icon and select new. Now I have another icon to the right of it. This should be availabel surly?
But, and I don't know how I managed it. I created another macro. Using my grey hairs on visual basic, I amended the routeen but I don't get exactly as I suspect. In my new code I get 11 tabs ranging from Test-3 through to Test-13, but I have said for I = 1 to 11, so it should be Test-1, Test-2. Quote:
|
#12
|
|||
|
|||
![]() Code:
Dim i As Integer Dim Name As String Name = "Test-" Application.CopyObjectsWithCells = False For i = 1 To 11 Sheets("SH-1").Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = "Test- " & Sheets.Count Next i Application.CopyObjectsWithCells = True 'reset End Sub |
#13
|
|||
|
|||
![]()
No that is just the same. Besides why create a variable string called Name and not use it? I still get Test-3, Test-4, Test-5.Test-6 etc.
I changed it to For i = 5000 To 5001 and I get Test-3, Test-4 |
#14
|
|||
|
|||
![]()
Sorry about that. Believe it or not, my old eyes didn't see that variable.
Code:
Option Explicit Sub mkeShs() Dim i As Integer Dim Name As String Name = "Test-" Application.CopyObjectsWithCells = False For i = 1 To 11 Sheets("SH-1").Copy After:=Sheets(Sheets.Count) ActiveSheet.Name = Name & i Next i Application.CopyObjectsWithCells = True 'reset End Sub |
#15
|
|||
|
|||
![]()
Just got there now as well. My days of VB are not wasted. I need a good book on excel 2021 or do I just need a good book on VBA?
I will need an Excel book I think so I can see the names of all the controlls etc. |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Exporting data to an excel template | Stevie23 | Excel Programming | 3 | 04-05-2018 12:12 AM |
![]() |
jassie | Excel | 1 | 03-06-2017 10:36 AM |
Excel Invoice Template | DMU62 | Excel | 19 | 11-02-2016 12:17 PM |
![]() |
kneghx | Excel | 1 | 05-13-2015 09:29 AM |
Excel Chart Template | mdarcey | Excel | 0 | 01-26-2012 08:43 AM |