+ Reply to Thread
Results 1 to 2 of 2

Increment Date based on Worksheet number

Hybrid View

  1. #1
    Registered User
    Join Date
    02-24-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    1

    Increment Date based on Worksheet number

    I need to create "Daily Worksheets". What I would like to do is have a front worksheet that requests "Start Date". Then I would like the next 90 sheets to increment the date slot by one day to reflect that start date + the worksheet number (i.e. start date = 2/20/2011 and the worksheet labeled "6" would have an autodate of 2/25/2011). Both the start date as well as each of the daily dates appear on cell B3. I saw an OFFSET command but could not figure out how to pull the worksheet label to add to it.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,642

    Re: Increment Date based on Worksheet number

    Hi there,

    Have a look at the attached workbook which contains the following code:

    
    Option Explicit
    
    
    Private Sub CreateWorksheets()
    
        Const sDATE_CELL_ADDRESS    As String = "B3"
        Const sFIRST_SHEET_NAME     As String = "Start Date"
        Const iNo_OF_SHEETS         As Integer = 90
    
        Dim dStartDate              As Double
        Dim dDate                   As Double
        Dim wks                     As Worksheet
        Dim i                       As Integer
    
        Application.ScreenUpdating = False
    
    '       Delete all existing sheets except the "Start Date" sheet
            Application.DisplayAlerts = False
                For Each wks In Worksheets
                    If wks.Name <> sFIRST_SHEET_NAME Then
                        wks.Delete
                    End If
                Next wks
            Application.DisplayAlerts = True
    
    '       Determine the starting date of the worksheets to be added
            dStartDate = Worksheets(sFIRST_SHEET_NAME).Range(sDATE_CELL_ADDRESS).Value
    
    '       Create and label new worksheets
            For i = 0 To iNo_OF_SHEETS - 1
    
    '           Determine the date of the worksheet to be added
                dDate = dStartDate + i
    
    '           Add new worksheet
                Worksheets.Add After:=Worksheets(Worksheets.Count)
    
                With ActiveSheet
    
    '               Format and populate the Date cell on the newly-added worksheet
                    With .Range(sDATE_CELL_ADDRESS)
                        .ColumnWidth = 12
                        .NumberFormat = "mm/dd/yyyy"
                        .Value = dDate
                    End With
    
    '               Assign the appropriate name to the new worksheet
                    .Name = Format(dDate, "mm-dd-yyyy")
    
                End With
    
            Next i
    
            Worksheets(sFIRST_SHEET_NAME).Activate
    
        Application.ScreenUpdating = True
    
    End Sub

    I think this will do what you want. The only thing is that worksheet names may not contain the "/" character, so the names of the newly-added sheets have the format "mm-dd-yyyy" rather than "mm/dd/yyyy".

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

    P.S. Do you need a "Confirm Action?" prompt to prevent you from accidentally deleting existing worksheets? If so, just let me know and I can add one.
    Attached Files Attached Files
    Last edited by Greg M; 02-24-2011 at 03:54 PM. Reason: P.S. added

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1