+ Reply to Thread
Results 1 to 3 of 3

Add suffix to existing filename

Hybrid View

  1. #1
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Add suffix to existing filename

    I am needing code to save an existing workbook while adding a suffix to the existing filename.

    i.e. "workbook.xls" becomes "workbook reviewed.xls"

    Also, if the routine can do it regardless of the existing extension; may be xls, xlsx, or xlsm, that would be helpful.

    Thank you,
    Delain

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Add suffix to existing filename

    This should get you nearly there. Concatenate the strings to build your filename and replace it and the file type from a recorded File > SaveAs macro.
    Sub ken()  
      Dim fn As String
      fn = ThisWorkbook.FullName
      Debug.Print fn, ThisWorkbook.Path
      Debug.Print GetBaseName(fn), GetFileExt(fn)
      Debug.Print ThisWorkbook.FileFormat
    End Sub
    
    Function GetBaseName(filespec As String)
      Dim FSO As Object
      Set FSO = CreateObject("Scripting.FileSystemObject")
      GetBaseName = FSO.GetBaseName(filespec)
    End Function
    
    Function GetFileExt(filespec As String)
      Dim FSO As Object, s As String
      Set FSO = CreateObject("Scripting.FileSystemObject")
      s = FSO.GetExtensionName(filespec)
      Set FSO = Nothing
      GetFileExt = s
    End Function

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,697

    Re: Add suffix to existing filename

    Another way if it is for the workbook you are in.
    Saves the workbook in the same folder with the new name.
    Sub jolivanes()
        Dim a As String, aa As String
        a = Trim(Split(ActiveWorkbook.Name, ".")(0))
        aa = Trim(Split(ActiveWorkbook.Name, ".")(1))
        ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & a & " Reviewed." & aa
        'ActiveWorkbook.Close    '<----- ??????	
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Formula to return filename in folder based on partial filename
    By Rerock in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-26-2014, 12:28 PM
  2. [SOLVED] Save with a new filename, replace existing string
    By Hjahren in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-12-2014, 08:53 AM
  3. [SOLVED] Changing a positive or negative value based on the input's suffix, then removing suffix
    By Theredwind in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-06-2014, 05:19 AM
  4. Script to open filename with suffix at the end of it?
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-12-2013, 02:15 PM
  5. [SOLVED] Runtime 1004 after prompting for filename but not when filename hard coded.
    By cgoodenough in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-04-2013, 03:46 AM
  6. [SOLVED] Rewrite existing code to include date in filename
    By MissaLissa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-02-2013, 01:03 PM
  7. Replies: 0
    Last Post: 11-01-2012, 09:28 AM

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