automatically copy and name sheets from template

prw79

New Member
Joined
Aug 5, 2022
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
I have a code (see below) that would copy a template sheet and rename them from a list that is generated from another range of all days in a month. When i switch months it automatically updates the list that will generate the sheets names. It works fine except that it expects 31 days (the range length i suppose). because when i switch to a month with lesser days it gives an error: 'Run-time error 1004: Application-defined or object-defined error'

code:
VBA Code:
Sub CopyTemplate()
    Dim wTemplate As Worksheet
    Dim wTOC As Worksheet
    Dim wCopy As Worksheet
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Set wTemplate = Worksheets("Template")
    Set wTOC = Worksheets("Setting")
    m = wTOC.Range("m1").End(xlDown).Row
    For r = 2 To m
        wTemplate.Copy After:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).Name = wTOC.Range("m" & r).Value
    Next r
    Application.ScreenUpdating = True
End Sub

I quess it has something to do with a 'if blank than stop' coding, but my knowledge isn't sufficient enough for this.
hope someone can help and thanks for reading.
 
Last edited by a moderator:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
How about
VBA Code:
Sub CopyTemplate()
    Dim wTemplate As Worksheet
    Dim wTOC As Worksheet
    Dim wCopy As Worksheet
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Set wTemplate = Worksheets("Template")
    Set wTOC = Worksheets("Setting")
    m = wTOC.Range("m1").End(xlDown).Row
    For r = 2 To m
        If wTOC.Range("M" & r) = "" Then Exit Sub
        wTemplate.Copy After:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).Name = wTOC.Range("m" & r).Value
    Next r
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub CopyTemplate()
    Dim wTemplate As Worksheet
    Dim wTOC As Worksheet
    Dim wCopy As Worksheet
    Dim r As Long
    Dim m As Long
    Application.ScreenUpdating = False
    Set wTemplate = Worksheets("Template")
    Set wTOC = Worksheets("Setting")
    m = wTOC.Range("m1").End(xlDown).Row
    For r = 2 To m
        If wTOC.Range("M" & r) = "" Then Exit Sub
        wTemplate.Copy After:=Worksheets(Worksheets.Count)
        Worksheets(Worksheets.Count).Name = wTOC.Range("m" & r).Value
    Next r
    Application.ScreenUpdating = True
End Sub
Again, brilliant!
 
Upvote 0
You're welcome & thanks for the feedback.

When marking a post as the solution, please mark the post with the solution, rather than your post saying it worked. I have changed it for you this time.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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