How to add new sheet with sheet name is the day of month

nhnn1986

Board Regular
Joined
Oct 12, 2017
Messages
92
Hi all

I have workbook with some sheet(MAIN, BC1,BC2, PL1,PL2,....,PL30) and now Iwant to have a macro that

when I type 201810 in sheet(Main).range(A1) then run macro
workbook add 31 newsheets with sheetname is the day of month like: 20181001, 20181002,.....,20181031

Next month, I type 201811 in sheet(Main).range(A1) then run macro
workbook first delete 31 sheets with sheetname of old month like: 20181001, 20181002,.....,20181031
And then add 30 newsheets with sheetname is the day of month like: 20181101, 20181102,.....,20181130

Please help me to do that.
Thanks in advanced
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this:
Code:
Sub MyMacro()

    Dim startDate As Date
    Dim endDate As Date
    Dim d As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False

    Sheets("Main").Activate
    
'   Get start date from year/month in cell A1
    On Error GoTo err_chk
    startDate = DateSerial(Left(Range("A1"), 4), Mid(Range("A1"), 5), 1)
    On Error GoTo 0
    
'   Find end date
    endDate = DateSerial(Year(startDate), Month(startDate) + 1, 0)

'   Delete old month dates
    For Each ws In Worksheets
        If Len(ws.Name) = 8 And Left(ws.Name, 2) = "20" Then
            Application.DisplayAlerts = False
            ws.Delete
            Application.DisplayAlerts = True
        End If
    Next ws
   
'   Loop through all days of month
    For d = startDate To endDate
        Sheets.Add After:=Sheets(Sheets.Count)
        ActiveSheet.Name = Format(d, "yyyymmdd")
    Next d
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
    
    Exit Sub
    
err_chk:
'   Return error message if invalid date value entered in cell A1
    MsgBox Range("A1") & " is not a valid year/month value!", vbOKOnly, "ERROR!"
    
End Sub
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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