+ Reply to Thread
Results 1 to 4 of 4

Rewrite existing code to include date in filename

  1. #1
    Registered User
    Join Date
    12-21-2012
    Location
    Flroida
    MS-Off Ver
    Excel 2016
    Posts
    39

    Rewrite existing code to include date in filename

    I'm using the following code and need to change it so that the date is added at the end of the new file name. I tried adding this: & Format(Date, "YYYYMMDD") & ".xlsx" but can't seem to get it in the correct location or maybe that just won't work. Can someone please help me rewrite the code?

    Sub RenameCMAnalysis()
    Dim fPATH As String, fName As String, NwNAME As String, NUM As Long

    fPATH = "\\ltcgsrv1\Cognos\Cognos Report Output\Deck Reports\CM Analysis Reports\"

    fName = Dir(fPATH & "*-en-us*.*") 'grab first filename with a -en-us in that name

    Do While Len(fName) > 0
    NUM = 1
    Do
    NwNAME = Left(fName, InStr(fName, "-en-us") - 1) & Right(fName, Len(fName) - InStrRev(fName, ".") + NUM)
    If Len(Dir(fPATH & NwNAME)) = 0 Then Exit Do
    NUM = NUM + 1
    Loop
    Name fPATH & fName As fPATH & NwNAME
    fName = Dir(fPATH & "*-en-us*.*")
    Loop


    End Sub

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Rewrite existing code to include date in filename

    Hi, MissaLissa,

    please use Code-tags when displaying code here in the forum.

    You would build a new filename with extension and add the number to the end of this string (after the extension).

    Maybe try it like this:
    Please Login or Register  to view this content.
    Maybe the length for the original file name has to be adpated.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    12-21-2012
    Location
    Flroida
    MS-Off Ver
    Excel 2016
    Posts
    39

    Re: Rewrite existing code to include date in filename

    Hi Holger

    Thanks for the reply. Your changes to the code just added the number 1 to the end of filename, however you did point me in the correct direction on where to add the Date.

    Missa

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,199

    Re: Rewrite existing code to include date in filename

    Hi, Missa,

    and after I had answered I realized that the thread title was like inserting Date while the code inserted a number. Glad you could solve it on your own and thanks for the feedback. Maybe you can post the relevant lines of code here for other users as a reference?

    Ciao,
    Holger

+ 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