I have been working on a Macro
that will automatically add new Annual Worksheets
when the Calendar Year
Changes. My current Code
is as follows:
Option Explicit
Sub addAnnualWkst()
Dim ws As Worksheet
Dim wsM As Worksheet
Dim strName As String
Dim strNamePreYr As String
Dim bCheck As Boolean
Dim pID As String
Dim rw
Set propIDs = ThisWorkbook.Names("propIDs").RefersToRange
Set actStatus = ThisWorkbook.Names("actStatus").RefersToRange
On Error Resume Next
Set wsM = Worksheets("WkstMaster")
For rw = 1 To propIDs.Count
If propIDs.Cells(rw, 1).Value2 <> vbNullString Then
If actStatus.Cells(rw, 1).Value2 = True Then
pID = propIDs.Cells(rw, 1).Value2
strName = pID & "_" & (Format(Date, "yyyy"))
strNamePreYr = pID & "_" & (Format(Date, "yyyy") - 1)
bCheck = Len(Sheets(strName).Name) > 0
Debug.Print pID, strName, strNamePreYr, bCheck
If bCheck = False Then
'add new sheet after Previous Year's Worksheet
wsM.Copy After:=Sheets(strNamePreYr)
ActiveSheet.Name = strName
End If
End If
End If
Next
Set wsM = Nothing
End Sub
the code above is based in part on on a Macro
I found in a Tutorial
I found and the
Module Code
is:
Option Explicit
Sub AddMonthWkst()
Dim ws As Worksheet
Dim wsM As Worksheet
Dim strName As String
Dim bCheck As Boolean
On Error Resume Next
Set wsM = Sheets("Wkst_Master")
strName = Format(Date, "yyyy_mm")
bCheck = Len(Sheets(strName).Name) > 0
If bCheck = False Then
'add new sheet after Instructions
wsM.Copy After:=Sheets(1)
ActiveSheet.Name = strName
End If
Set wsM = Nothing
End Sub
The above 'code' works as advertised! bCheck
returns False
and the new worksheet is added. I am able to rename the worksheet tab from the current month 05
to the previous month 04
, save and close the workbook and when I reopen the workbook a new worksheet is automatically added with the 05
month extension.
I modified the code slightly to fit my needs and incapsulated that code in a subroutine
I successfully use in different parts of the application where I select pIDs
based on actStatus
.
I have active Worksheet Tabs
for the various PropIDs
as shown in this image:
When I run the Macro
the Immediate Window
shows ALL Active pIDs
with a pCheck Value
as True
when the pID "Rev"
should return a value of False
because pID "Rev"
does not have a WorkSheet
for the current year!
As one can see for the Immediate window
screenshot below, all the relevant pIDs
are there!
If I disable the 'On Error Resume Next' line I get the Runtime Error: 9, Script out of range
error and with or without the Error Trap
the worksheet is not added. the Error
happens at the highlighted
line of code
.
Please help me to resolve this issue. I know it is something simple I am missing! Thanks in advance.