Solved

Excel Macro to create a new worksheet

Posted on 2014-09-30
6
440 Views
Last Modified: 2014-09-30
Hello Experts Exchange
I have a worksheet that is call BLANK, and every month I have to create a spreadsheet that has a BLANK worksheet for every work day in the month.

Is there a way in a macro to copy the BLANK worksheet and give it the name of a work day in the month, and the macro to keep copying the BLANK worksheet until a worksheet is created for the whole month?

The format of the worksheet name should be for examples;
01 Sep
02 Sep
03 Sep

Regards

SQLSearcher
0
Comment
Question by:SQLSearcher
6 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40352078
I've kept it fairly generic so you can adapt it as you wish.

Sub CreateSheets()
Dim MyMonth As String, NumDays As Integer
MyMonth = "Sep"
NumDays = 30
For intday = 1 To NumDays
    Sheets("BLANK").Select
    Sheets("BLANK").Copy After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = Format(intday, "00") & " " & MyMonth
Next
End Sub

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40352108
This assumes you run the macro in the month you want to create the sheets for.

Sub CreateSheets()
    Dim sht As Worksheet
    Dim intDays As Integer
    Dim lngDays As Long
    
    On Error Resume Next
    Application.DisplayAlerts = False
    For Each sht In Worksheets
        sht.Delete
    Next
    Application.DisplayAlerts = True
    On Error GoTo 0
    intDays = Day(DateSerial(Year(Now), Month(Now) + 1, 1) - 1)
    Sheets(1).Name = "01 " & MonthName(Month(Now), True)
    
    For lngDays = 2 To intDays
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = Format(lngDays, "00") & " " & MonthName(Month(Now), True)
    Next
    
End Sub

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40352123
To create the sheets for next month.

Sub CreateSheets()
    Dim sht As Worksheet
    Dim intDays As Integer
    Dim lngDays As Long
    Dim dtNextMonth As Date
    
    dtNextMonth = DateAdd("m", 1, Date)
    
    On Error Resume Next
    Application.DisplayAlerts = False
    For Each sht In Worksheets
        sht.Delete
    Next
    Application.DisplayAlerts = True
    On Error GoTo 0
    intDays = Day(DateSerial(Year(dtNextMonth), Month(dtNextMonth) + 1, 1) - 1)
    Sheets(1).Name = "01 " & MonthName(Month(dtNextMonth), True)
    
    For lngDays = 2 To intDays
        Sheets.Add After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = Format(lngDays, "00") & " " & MonthName(Month(dtNextMonth), True)
    Next
    
End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 12

Expert Comment

by:James Elliott
ID: 40352177
Another approach:

Public Const MONTH_NUM As Integer = 9 'change this to suit whichever month you want to create a workbook for

Sub CreateDaySheets()

i = 1

Do

    Sheets("BLANK").Copy After:=Sheets(Sheets.Count)
    
    Sheets(Sheets.Count).Name = Format(i, "0#") & " " & Left(MonthName(MONTH_NUM), 3)

    i = Day(DateAdd("d", 1, DateSerial(2014, MONTH_NUM, i)))

Loop Until i = 1

End Sub

Open in new window

0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40352203
Hi,

If you only want workdays

pls try

Sub CreateSheetsThisMonth()
Dim MyMonth, NumDays As Integer
MyMonth = Month(Now())
intDays = Day(DateSerial(Year(Now()), Month(Now()) + 1, 1) - 1)
For intday = 1 To intDays
    If WorksheetFunction.Weekday(DateSerial(Year(Now()), Month(Now()), intday), 2) < 6 Then
        Sheets("BLANK").Select
        Sheets("BLANK").Copy After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = Format(intday, "00") & " " & MonthName(MyMonth, True)
    End If
Next
End Sub

Sub CreateSheetsNextMonth()
Dim MyMonth, NumDays As Integer
MyMonth = Month(Now() + 1)
intDays = Day(DateSerial(Year(Now()), Month(Now()) + 2, 1) - 1)
For intday = 1 To intDays
    If WorksheetFunction.Weekday(DateSerial(Year(Now()), Month(Now() + 1), intday), 2) < 6 Then
        Sheets("BLANK").Select
        Sheets("BLANK").Copy After:=Sheets(Sheets.Count)
        Sheets(Sheets.Count).Name = Format(intday, "00") & " " & MonthName(MyMonth, True)
    End If
Next
End Sub

Open in new window

Regards
0
 

Author Closing Comment

by:SQLSearcher
ID: 40352312
Thank you, for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now