+ Reply to Thread
Results 1 to 3 of 3

renaming new worksheet with time stamp and text

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2011
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    14

    Question renaming new worksheet with time stamp and text

    I am new to VBA and currently working on a dashboard-ish project. My code needs to create a new tab to store downloaded data from a SQL database AND rename the tab as "Financial_Data_MM-DD-YY" where MM-DD-YY represent the current date.

    What I have already complete is displayed below. Can anyone help a brotha out and explain how to add text into this naming convention?

    Sub CreateRenameDataSheet()
    
       Dim i As Integer, Cntr As Integer
            
        Sheets.Add
        For i = 1 To Sheets.Count
            If Left(Sheets(i).Name, 10) = Format(Date, "DD-MM-YYYY") Then
                Cntr = Cntr + 1
            End If
        Next
        If Cntr = 0 Then
            ActiveSheet.Name = Format(Date, "DD-MM-YYYY")
        Else
            ActiveSheet.Name = Format(Date, "DD-MM-YYYY") & " (" & Cntr & ")"
        End If
            
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: renaming new worksheet with time stamp and text

    One way:

    Sub CreateRenameDataSheet()
        Const sWks      As String = "Financial_Data_"
        Dim sDate       As String
        Dim wks         As Worksheet
        Dim n           As Integer
    
        sDate = Format(Date, "DD-MM-YYYY")
    
        For Each wks In Worksheets
            If InStr(wks.Name, sDate) Then n = n + 1
        Next
    
        If n = 0 Then
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sWks & sDate
        Else
            Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sWks & sDate & " (" & n & ")"
        End If
    End Sub
    Last edited by shg; 05-22-2012 at 06:48 PM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-21-2011
    Location
    Portland, OR
    MS-Off Ver
    2010
    Posts
    14

    Re: renaming new worksheet with time stamp and text

    Thanks! Works like a charm

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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