Hello all,
I've been working on a Table of Contents (TOC) sheet with hyperlinks that can be copy and pasted into any workbook and will automatically populate with the sheet names of that workbook. It can also duplicate any sheet named "Master Template" with a button that references a macro that is embedded in the sheet. This will ask for a sheet name, and if the name is valid (no non-case sensitive repetition, etc.) it will create a copy of the "Master Template" sheet, rename it, and add it after the last tab. This works fine in the original sheet, but when I copy and paste the sheet to a new workbook, the macro referenced by the button is still the one on the original sheet, not on the copy itself. It even opens the old sheet when the button is pressed, and runs the macro there. The "Master Template" being copied is from the new workbook, and it is pasted in the new workbook. However, when checking to see if the name is valid, it references the sheet names in the old workbook, as well as the location of the last tab from the old workbook to know where to insert the duplicated sheet.
For example, consider the "New" that workbook has the TOC sheet copied from the "Old" workbook. If "Old" has 3 sheets and "New" has 5, a valid name entry will duplicate the "Master Template" from "New" and paste it after the 3rd sheet of "New". If "Old" has more sheets than "New" then running the macro and giving a valid name will give a sub out of bounds exception. Additionally, if a sheet name is used in "Old" but not in "New" it will be considered invalid.
This, of course, can be fixed by right clicking the button and switching the macro it references to the one located in the "New" sheet, but I would like to avoid making the user go into the backend at all if possible. I think I read something about an excel extension that could fix this problem, but I have no experience with that.
I have pasted the macro below. You can create an excel workbook with a sheet named "TOC" that has a button and a sheet named "Master Template" to be duplicated. ***Make sure you paste the macro within the TOC sheet itself and not a module. Then copy the sheet itself to a new workbook that has a "Master Template" sheet as well. You can experiment with different sheet names and amounts and it might help you visualize what I have explained above.
(I apologize if I explained it badly. I wrote out something nicer, but I left my browser open and the page refreshed and deleted what I had. I did my best to retouch on all my thoughts.)
Thanks for your time. I hope there is a simple solution to this.
Cheers.
I've been working on a Table of Contents (TOC) sheet with hyperlinks that can be copy and pasted into any workbook and will automatically populate with the sheet names of that workbook. It can also duplicate any sheet named "Master Template" with a button that references a macro that is embedded in the sheet. This will ask for a sheet name, and if the name is valid (no non-case sensitive repetition, etc.) it will create a copy of the "Master Template" sheet, rename it, and add it after the last tab. This works fine in the original sheet, but when I copy and paste the sheet to a new workbook, the macro referenced by the button is still the one on the original sheet, not on the copy itself. It even opens the old sheet when the button is pressed, and runs the macro there. The "Master Template" being copied is from the new workbook, and it is pasted in the new workbook. However, when checking to see if the name is valid, it references the sheet names in the old workbook, as well as the location of the last tab from the old workbook to know where to insert the duplicated sheet.
For example, consider the "New" that workbook has the TOC sheet copied from the "Old" workbook. If "Old" has 3 sheets and "New" has 5, a valid name entry will duplicate the "Master Template" from "New" and paste it after the 3rd sheet of "New". If "Old" has more sheets than "New" then running the macro and giving a valid name will give a sub out of bounds exception. Additionally, if a sheet name is used in "Old" but not in "New" it will be considered invalid.
This, of course, can be fixed by right clicking the button and switching the macro it references to the one located in the "New" sheet, but I would like to avoid making the user go into the backend at all if possible. I think I read something about an excel extension that could fix this problem, but I have no experience with that.
I have pasted the macro below. You can create an excel workbook with a sheet named "TOC" that has a button and a sheet named "Master Template" to be duplicated. ***Make sure you paste the macro within the TOC sheet itself and not a module. Then copy the sheet itself to a new workbook that has a "Master Template" sheet as well. You can experiment with different sheet names and amounts and it might help you visualize what I have explained above.
(I apologize if I explained it badly. I wrote out something nicer, but I left my browser open and the page refreshed and deleted what I had. I did my best to retouch on all my thoughts.)
Thanks for your time. I hope there is a simple solution to this.
Cheers.
Code:
Public Sub CopySheet()
Dim templateHidden As Boolean
Application.ScreenUpdating = False
templateHidden = False
'Is the Master Template Hidden?
If Worksheets("Master Template").Visible = False Then
templateHidden = True
End If
newName = Application.InputBox("Enter name of New Sheet:", "New Sheet Creation", Type:=2)
'Quit if cancel was pushed
If newName = "" Or StrPtr(newName) = 0 Or newName = vbNull Or newName = False Then
Exit Sub
End If
'If name exists already, keep asking for a new one until you get one that works
tableArray = Range("C6:C250")
Do While isInArray(newName, tableArray) Or newName = Me.Name
newName = Application.InputBox("That name already exists. Please try again with another name:", "New Sheet Creation", Type:=2)
'Quit if Cancel was pushed
If newName = "" Or StrPtr(newName) = 0 Or newName = vbNull Or newName = False Then
Exit Sub
End If
Loop
'Create the new sheet and name it accordingly
'If Master Template is hidden, unhide the duplicate
If templateHidden Then
Sheets("Master Template").Visible = True
Sheets("Master Template").Copy after:=Sheets(ThisWorkbook.Sheets.Count)
Sheets("Master Template").Visible = False
Else
Sheets("Master Template").Copy after:=Sheets(ThisWorkbook.Sheets.Count)
End If
ActiveSheet.Name = newName
Application.ScreenUpdating = True
End Sub