Solved

Excel Macro to create a new worksheet

Posted on 2014-09-30
6
441 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

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.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

831 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