Solved

Excel Macro to create a new worksheet

Posted on 2014-09-30
6
435 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 45

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 45

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 48

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

708 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

15 Experts available now in Live!

Get 1:1 Help Now