?
Solved

Excel Macro to create a new worksheet

Posted on 2014-09-30
6
Medium Priority
?
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 49

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 49

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

719 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