VBA Code for creating multiple worksheets
Hey guys -
Ok, so I found some code on the Internet that I have been trying to tweak to my liking but I'm having some trouble. Here's what I am trying to do.
I want to create a worksheet for each day of a given month and name the worksheet for each respective day. The naming convention would work like: 10.01.10 Data, 10.02.10 Data, etc.
Also, I would like to create another set of worksheets that are named with the same date convention but with a different name: 10.01.10 Summary, 10.02.10 Summary.
Here's the code I found which helps me solve one part of my problem although the part that adds a new workbook is a little annoying because I would prefer that when I opened up a new workbook in Excel that the macro modify this open workbook instead of opening another workbook. But I'm unsure how to go about the same steps for creating the worksheets titled 10.01.10 Summary.
Here's the code:
Sub CreateNewBook()
Dim WS As Worksheet ', WB As Workbook
Dim MonthX As Date, Control As Variant, DaysInMonth As Byte, I As Byte, NewSheetCount As Byte, OldSheetCount As Byte
OldSheetCount = 1
Control = InputBox("Enter month in the form of mm/yyyy.", "Month Entry", Month(Date) & "/" & Year(Date))
If IsDate(Control) Then
MonthX = CDate(Control)
DaysInMonth = Day(DateSerial(Year(MonthX), Month(MonthX) + 1, 0))
Application.SheetsInNewWorkbook = DaysInMonth
'Set WB = Workbooks.Add
'Set WS = Worksheets.Add
I = 1
For Each WS In ActiveWorkbook.Worksheets
WS.Name = Month(MonthX) & " " & I
I = I + 1
Next
Else
MsgBox "Error while inputing start date."
End If
Application.SheetsInNewWorkbook = OldSheetCount
End Sub
Comment on JBeaucaire's post
JB - I use Excel 2007 and when I run your code in my workbook, it doesn't run at all but when I run it in Excel 2003 (compatibility version) it runs fine. Maybe one or more of the command lines are slightly different from 03 to 07?
Comment on JBeaucaire's post
I'll have to double-check when I'm back at the office tomorrow... yeah, my macros are definitely enabled (I have about 15 of them that I run daily) but for some reason it wasn't running. Strangely, it gave no error either... thx, JB.
Comment on ScottGem's post
Thanks, Scott. Actually, the purpose of this macro is to create a workbook for each day of the month and dump data into each daily WS. This code saves me from doing it manually.
Comment on JBeaucaire's post
JB - I read this post after I fixed the problem. Still not sure what the issue was but I got the code to work in an existing module I built. Thx again, that was really helpful stuff.