Macro to Copy Tab Multiple Times w/ Reference to Sequential Cells

hebehemonkey

New Member
Joined
Oct 12, 2017
Messages
3
Hi Everyone,

I have to create 79 tabs in excel using a standard template [see below], but that references sequential values in a separate worksheet. We will call the template worksheet "Template" and the worksheet that contains the reference values "Reference". I need help creating a macro to do the following:

  1. Copy the template 79 times.
  2. Name each new worksheet according to the sequential rows in column F of "Reference" (so the first copy of "Template" would have a name defined in F3 in the "Reference" sheet. the second copy of "Template" would have a name defined by F4, etc. etc. etc).
  3. Set the values of cells in the new worksheets (i.e. the copies of "Template") equal to sequential rows in Column A, B, C, D, E of the worksheet "Reference", such that copy 1 of the "Template" references cells A3, B3, C3, D3 and E3; copy 2 of the "Template" references cells A4, B4, C4, D4, E4; etc.

F4M6lRA.png


I tried to start something, but my skills (and knowledge of the correct vocabulary) have my hopelessly lost. Can anyone help?


Sub CopyWorkSheets()
Dim xNumber As Integer
Dim xWsName As String
On Error Resume Next
xTitleId = "Enter Value"
xWsName = Application.InputBox("Copy worksheet name", xTitleId, , Type:=2)
xNumber = Application.InputBox("Copy number", xTitleId, , Type:=1)
For i = 1 To xNumber
Application.ActiveWorkbook.Sheets(xWsName).Copy _
After:=Application.ActiveWorkbook.Sheets(xWsName)
Next
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Hi & welcome
Try this
Code:
Sub CopyTemplate()

    Dim Cnt As Long
    Dim RefSht As Worksheet
    
    Set RefSht = Sheets("[COLOR=#0000ff]Reference[/COLOR]")
    For Cnt = 3 To 81
        Sheets("[COLOR=#0000ff]Template[/COLOR]").Copy after:=Sheets("[COLOR=#0000ff]Template[/COLOR]")
        With ActiveSheet
            .Name = RefSht.Range("F" & Cnt).Value
            .Range("[COLOR=#ff0000]B4[/COLOR]").Value = RefSht.Range("A" & Cnt)
            .Range("[COLOR=#ff0000]B5[/COLOR]").Value = RefSht.Range("B" & Cnt)
            .Range("[COLOR=#ff0000]B6[/COLOR]").Value = RefSht.Range("C" & Cnt)
            .Range("[COLOR=#ff0000]B7[/COLOR]").Value = RefSht.Range("D" & Cnt)
            .Range("[COLOR=#ff0000]B8[/COLOR]").Value = RefSht.Range("E" & Cnt)
        End With
    Next Cnt

End Sub
Make sure that the sheet names in blue are correct.
Also check that the range references in red are correct, I've taken a guess based on the image you supplied
 
Upvote 0
Hi & welcome
Try this
Code:
Sub CopyTemplate()

    Dim Cnt As Long
    Dim RefSht As Worksheet
    
    Set RefSht = Sheets("[COLOR=#0000ff]Reference[/COLOR]")
    For Cnt = 3 To 81
        Sheets("[COLOR=#0000ff]Template[/COLOR]").Copy after:=Sheets("[COLOR=#0000ff]Template[/COLOR]")
        With ActiveSheet
            .Name = RefSht.Range("F" & Cnt).Value
            .Range("[COLOR=#ff0000]B4[/COLOR]").Value = RefSht.Range("A" & Cnt)
            .Range("[COLOR=#ff0000]B5[/COLOR]").Value = RefSht.Range("B" & Cnt)
            .Range("[COLOR=#ff0000]B6[/COLOR]").Value = RefSht.Range("C" & Cnt)
            .Range("[COLOR=#ff0000]B7[/COLOR]").Value = RefSht.Range("D" & Cnt)
            .Range("[COLOR=#ff0000]B8[/COLOR]").Value = RefSht.Range("E" & Cnt)
        End With
    Next Cnt

End Sub
Make sure that the sheet names in blue are correct.
Also check that the range references in red are correct, I've taken a guess based on the image you supplied


Thanks! I tried it out, and it starts to work, but throws an error on the line,

.Name = Refsht.Range("C" & Cnt).Value

Error: "Run-time error '424': Object Required". I tried playing around and I can't figure it out.
 
Upvote 0
What was the value of Cnt when it failed & what was the value of F & Cnt?
Ie if Cnt was 11 what was the value of F11
 
Upvote 0
What was the value of Cnt when it failed & what was the value of F & Cnt?
Ie if Cnt was 11 what was the value of F11


Figured it out! It was F3, which prompted me to look at the values of F, and I realized that the character length was too long for a tab name. I shortened the cell values and it worked! Thank you!!!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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