(VBA) Copy a template sheet, rename based on an active cell offset, and populate fields on new sheet

kaaryy

New Member
Joined
Aug 17, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
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.

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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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