I have:
1. A sheet with a list, including index #, name, location, and "Create Template" hyperlink; and
2. A template sheet within the same workbook, to be used for each entry on the list.
My goal is to create a macro that, once the "Create Template" hyperlink is clicked, it:
1. Creates a copy of that template sheet;
2. Renames that sheet to match the index #; and
3. On the new sheet, completes fields pulling over that index #, name, location.
The hyperlink part, as well as the creating the new sheet are functional. Right now I'm stuck at renaming the sheet and pulling over the fields. The problem I'm facing with researching a solution is that it cannot be static references.
- As multiple people will use this book (in separate versions), I only need them to "Create Template" for certain records they choose. I mean, they will click "Create Template" for 1 record out of a total of 50 in the list. So, I don't need it to loop, and I don't need to pre-create these templates.
- Thus I was thinking I need to use an ActiveCell.Offset within the code in order to rename the sheet. So, I click on the "Create Template" hyperlink in cell D4, it should rename the sheet for the value (index #) in A4. And so on.
- However, I was having trouble with that, so I resorted to an input box where the user enters the index #. Not preferable due to human error, but OK.
- Next, based on the index #, I would like it to pull data from the List sheet into the newly created template sheet. I was thinking perhaps there's a way to match the sheet name to a list, and then sort of vlookup the data that way. I tried a few versions of a vlookup formula, but was unable to get the sheet name to "read".
This is current code I have below. Note that I was attempting to use Application.ScreenUpdating and the current_sheet variable (= the List sheet) in order to retain the List sheet as active, so that I may use an active cell offset. However, this may be unnecessary.
1. A sheet with a list, including index #, name, location, and "Create Template" hyperlink; and
2. A template sheet within the same workbook, to be used for each entry on the list.
My goal is to create a macro that, once the "Create Template" hyperlink is clicked, it:
1. Creates a copy of that template sheet;
2. Renames that sheet to match the index #; and
3. On the new sheet, completes fields pulling over that index #, name, location.
The hyperlink part, as well as the creating the new sheet are functional. Right now I'm stuck at renaming the sheet and pulling over the fields. The problem I'm facing with researching a solution is that it cannot be static references.
- As multiple people will use this book (in separate versions), I only need them to "Create Template" for certain records they choose. I mean, they will click "Create Template" for 1 record out of a total of 50 in the list. So, I don't need it to loop, and I don't need to pre-create these templates.
- Thus I was thinking I need to use an ActiveCell.Offset within the code in order to rename the sheet. So, I click on the "Create Template" hyperlink in cell D4, it should rename the sheet for the value (index #) in A4. And so on.
- However, I was having trouble with that, so I resorted to an input box where the user enters the index #. Not preferable due to human error, but OK.
- Next, based on the index #, I would like it to pull data from the List sheet into the newly created template sheet. I was thinking perhaps there's a way to match the sheet name to a list, and then sort of vlookup the data that way. I tried a few versions of a vlookup formula, but was unable to get the sheet name to "read".
This is current code I have below. Note that I was attempting to use Application.ScreenUpdating and the current_sheet variable (= the List sheet) in order to retain the List sheet as active, so that I may use an active cell offset. However, this may be unnecessary.
VBA Code:
Sub Insert_Template()
Dim current_sheet As Worksheet
Application.ScreenUpdating = False
Set current_sheet = ActiveSheet
With ThisWorkbook
Sheets("Template").Copy After:=Sheets(Sheets.Count)
Application.ScreenUpdating = True
'input box here in order to rename sheet by index #, which is not ideal. I would prefer to automate based on which "Create Template" link I've clicked.
Response = InputBox("Index #", vbOKCancel)
If Response = False Or Response = "" Then
MsgBox "Invalid Name"
Exit Sub
Else
ActiveSheet.Name = Response
End If
Response = ""
'having current_sheet active is only important if I need to use active cell reference.
current_sheet.Activate
End With
End Sub