Solved

Excel Macro to create a new worksheet

Posted on 2014-09-30
6
443 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel bubble chart with most common ranges of values 12 43
Cost allcocation ... 10 23
Excel Macro 9 22
Index Match Formula VBA 6 21
: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

820 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