Ask Experts Questions for FREE Help !
Ask
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #1

    Oct 25, 2010, 11:47 AM
    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

    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Nov 4, 2010, 03:20 PM

    1) Name the sheet in your workbook that you want to copy TEMPLATE.

    2) Then run this shorter version:
    Code:
    Option Explicit
    
    Sub SheetCopy()
    Dim strDate As String
    Dim NumDays As Long
    Dim i As Long
    Dim sh As Object
    Dim wsBase As Worksheet
    
    On Error GoTo EndIt
        
    ' The Do statement to captures Month/Year via Input Box
    ' and return number of days in the month to the NumDays variable
    
        Do
            strDate = Application.InputBox( _
                Prompt:="Please enter month and year: mm/yyyy", _
                Title:="Month and Year", _
                Default:=Format(Date, "mm/yyyy"), _
                Type:=2)
     
            If strDate = "False" Then Exit Sub
            If IsDate(strDate) Then Exit Do
            If MsgBox("Please enter a valid date, such as ""01/2010""." _
                & vbLf & vbLf & "Shall we try again?", vbYesNo + vbExclamation, _
                "Invalid Date") = vbNo Then End
        Loop
        
        Application.ScreenUpdating = False
        NumDays = Day(DateSerial(Year(strDate), Month(strDate) + 1, 0))
        Set wsBase = Sheets("Template")
        
    ' For each day, the For statement below copies the template sheet 'n' times
    
        For i = 1 To NumDays
            wsBase.Copy After:=Sheets(Sheets.Count)
            ActiveSheet.Name = Format(DateSerial(Year(strDate), Month(strDate), i), "mm.dd.yy")
        Next i
    
    EndIt:
        Application.ScreenUpdating = True
    End Sub
    Attached Files
  1. File Type: xls CopyTemplate.xls (27.5 KB, 289 views)
  2. jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #3

    Nov 4, 2010, 04:44 PM
    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?
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #4

    Nov 4, 2010, 07:00 PM

    Doesn't run at all? Try opening the VBEditor and using F8 to run the code one line at a time, spot the line of code where it does the wrong thing.

    2007 means you installed it into an xlsm file, right?
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #5

    Nov 4, 2010, 07:18 PM

    I had a colleague run it on his xl2007, worked for him, too. Macros enabled on yours?
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #6

    Nov 4, 2010, 09:30 PM
    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.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #7

    Nov 5, 2010, 02:16 AM

    It's not strange that it gives no error. I have an On Error Goto EndIt line in there so when anything goes wrong it exits gracefully.

    The F8 trick will reveal that to you and you'll exactly which line of code failed and generated an error.
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #8

    Nov 5, 2010, 04:06 AM

    Make sure you are saving the file as an xlsm. I had a similar issue with VBA code not running until I did that.

    But I have to ask what the purpose of this spreadsheet is. It sounds to me like you may be ready to move this data up to a real database.
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #9

    Nov 5, 2010, 08:10 AM
    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.
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #10

    Nov 5, 2010, 08:12 AM
    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.
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #11

    Nov 5, 2010, 09:42 AM

    Jakester said 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.

    And again I ask what the purpose of this spreadsheet is. If you have separate data by day, this can usually be done more easily in a database than a spreadsheet. Spreadsheets are great and have a definite place, but there frequently comes a time when putting DATA into a database makes more sense.

    BTW, please don't use the Comments for follow-up. Use the Answer options instead.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #12

    Nov 5, 2010, 10:02 AM

    In general, I have to agree with Scott. But I know it's hard to walk away from Excel after using it for so long. ;)

    I also find individual day sheets for data a pretty "humanistic" approach to data management, too. Even if you stay with Excel, I would put all my data into a single "database sheet" in my workbook.

    With the data in a single sheet, I can use normal reporting, filtering, charting tools to extract meaningful data from the set as a whole. Your layout makes that near-on a marathon event to extract complex reports. The data is too spread out.

    And with the AUTOFILTER, it's a breeze to still see any one day of data in the master database sheet without having to navigate all those individual sheets.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

Excel 2003 Macro VBA Hide / Unhide Multiple Sheets [ 5 Answers ]

I'd like to assign a macro button to hide / unhide a range of sheets in a workbook. I have tried various codes from web but have not been successful. (acutally one of the codes worked if I selected 'run' from the macro menu but does not run if button is clicked). I have tried 'recording'...

Macros for sequential numbering in multiple worksheets in excel with [ 1 Answers ]

I have a workbook with worksheets for each day of the month. I need to subtract one day to = equal number of days left in the month. Ex(May 1st there are 31 days left in month, May 2nd, worksheet two, there are 30 days left in the month, and so on and so forth.) Is there a way to have these days...

VBA Code for Creating a Chart [ 1 Answers ]

Ok, here's my problem. I have built a macro that creates a chart for me from certain columns of data. The columns are always the same ones and the data I contemplate always begins in the same row but the end data is a variable. Also, the last row always contains a Totals Row which is something...

VBA code [ 2 Answers ]

Once the month ends, cell "A1" must tick over to zero, where "A1" is used by me as a counter to show how much has been deliverd to date for the month.

Using one Excel list table in multiple worksheets [ 2 Answers ]

I have a workbook containing 13 worksheets. Sheet 13 contains two list tables. I need to link sheets 1 through 12 to the list tables in sheet 13 so that only one set of lists has to be updated when changes occur. Please help me...


View more questions Search