Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2025, 07:10 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default Excel template


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
Reply With Quote
  #2  
Old 01-09-2025, 09:05 AM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

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
The MAIN worksheet has a list of SHEET NAMES beginning in A1 and going down the same column. The TEMPLATE sheet is positioned as the first sheet in the workbook (not that it matters) and the MAIN sheet is in the second position.

When you run the macro it auto creates all of the sheets in the list based on the TEMPLATE sheet.

See attached.
Attached Files
File Type: xlsm Template Create Tabs.xlsm (21.3 KB, 4 views)
Reply With Quote
  #3  
Old 01-09-2025, 02:52 PM
Italophile Italophile is online now Excel template Windows 11 Excel template Office 2021
Expert
 
Join Date: Mar 2022
Posts: 542
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

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).
Reply With Quote
  #4  
Old 01-09-2025, 04:18 PM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

"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.
Reply With Quote
  #5  
Old 01-12-2025, 04:13 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default

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.
Reply With Quote
  #6  
Old 01-12-2025, 01:24 PM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

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.
Reply With Quote
  #7  
Old 01-13-2025, 07:44 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default

This is where I have got to. Attached. I vaugly remember something about changing the file from xls to xlsm or something.
Attached Images
File Type: jpg Macros-1.jpg (179.3 KB, 18 views)
Reply With Quote
  #8  
Old 01-13-2025, 09:02 AM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

Ok, so the button control remains the upper left corner.
Reply With Quote
  #9  
Old 01-15-2025, 03:53 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default

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.
Attached Files
File Type: xlsx TempMacro.xlsx (15.0 KB, 3 views)
Reply With Quote
  #10  
Old 01-15-2025, 04:49 AM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

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
Reply With Quote
  #11  
Old 01-15-2025, 07:20 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default

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:
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 & Sheets.Count
Next i
Application.CopyObjectsWithCells = True 'reset
End Sub
Reply With Quote
  #12  
Old 01-15-2025, 08:15 AM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

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
Reply With Quote
  #13  
Old 01-15-2025, 08:48 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default

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
Reply With Quote
  #14  
Old 01-15-2025, 09:01 AM
Logit Logit is offline Excel template Windows 10 Excel template Office 2007
Expert
 
Join Date: Jan 2017
Posts: 587
Logit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the roughLogit is a jewel in the rough
Default

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
The previous macro didn't start at the number you were hoping for because 3 sheets already existed. So vba began with the next number after the already existing number of sheets.
Reply With Quote
  #15  
Old 01-15-2025, 09:57 AM
Pablo2000 Pablo2000 is offline Excel template Windows 11 Excel template Office 2021
Advanced Beginner
Excel template
 
Join Date: Dec 2023
Posts: 62
Pablo2000 is on a distinguished road
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting data to an excel template Stevie23 Excel Programming 3 04-05-2018 12:12 AM
Excel template excel 2013 template jassie Excel 1 03-06-2017 10:36 AM
Excel Invoice Template DMU62 Excel 19 11-02-2016 12:17 PM
Excel template Excel Default Template kneghx Excel 1 05-13-2015 09:29 AM
Excel Chart Template mdarcey Excel 0 01-26-2012 08:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:37 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft