Add consecutive year_month sheets

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Everybody, Need help to modify the code to create consecutive month sheets. ( if 2021_07 exists, then 2021_08 and so on).

VBA Code:
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("202106")
strName = Format(Date, "yyyymm")
'bCheck = Len(Sheets(strName).Name) > 0

If bCheck = False Then
'add new sheet after Instructions
    wsM.Copy Before:=Sheets(1)
    ActiveSheet.Name = strName
End If

Set wsM = Nothing
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try this:

PHP:
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("202106")
strName = Format(Date, "yyyymm")
bCheck = Evaluate("IsError('" & strName & "'!A1)")
If bCheck = True Then
'add new sheet after Instructions
    wsM.Copy Before:=Sheets(1)
    ActiveSheet.Name = strName
End If

Set wsM = Nothing
End Sub
 
Upvote 0
Hi Phuoc,
It adds a sheet of current month only, No sheets are added after further run . (Mine can still add sheet but in sheetname 202106 (2).
 
Upvote 0
Hi;

Try this

Code:
Sub AddNextMonth()
Dim ws As Worksheet
Dim wsM As Worksheet
Dim strName As String
Dim maxName As Long

For Each ws In ThisWorkbook.Sheets
    If IsNumeric(ws.Name) Then
        If CLng(ws.Name) > maxName Then maxName = CLng(ws.Name)
    End If
Next
If maxName > 0 Then
    strName = Format(DateAdd("m", 1, DateSerial(Int(maxName / 100), maxName Mod 100, 1)), "yyyymm")
    Set wsM = Sheets(CStr(maxName))
    wsM.Copy Before:=Sheets(1)
    ActiveSheet.Name = strName
End If

Set wsM = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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