+ Reply to Thread
Results 1 to 8 of 8

Save with a new filename, replace existing string

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Save with a new filename, replace existing string

    Hi,

    I have a document saved with the name "Q114 XXXXXX".
    I want to have a macro that replaces Q114 with Q214.

    Is that easy to make?

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: Save with a new filename, replace existing string

    See my reply below...seems like my reply initially doubleposted
    Last edited by vnzerem; 03-11-2014 at 12:19 PM.

  3. #3
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: Save with a new filename, replace existing string

    Lets assume that you have the Old file name in Column A and the New name in Column B. Make sure you have the complete name of the file you want to rename and to what you want.
    For example, I tried this on
    Column A
    C:\Users\Desktop\test.xlsm

    Column B
    C:\Users\Desktop\testrename.xlsm

    Hence you can list all the files you want to change and the macro will loop and change everything.
    The code below should rename the file to what you want.

    Select Code copy to clipboard
    Sub vnzerem()
        Dim oldfilename, newfilename, r
        r = 0
        Do
            r = r + 1
            oldfilename = Cells(r, 1)
            newfilename = Cells(r, 2)
            If oldfilename = "" Or newfilename = "" Then Exit Do
            Name oldfilename As newfilename
        Loop
    End Sub
    Don’t forget to click the star if this helped you, and don’t forget to mark the thread as solved

  4. #4
    Forum Contributor
    Join Date
    01-30-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2010
    Posts
    604

    Re: Save with a new filename, replace existing string

    Try this...

    Sub tester()
    
    Dim old_name As String, new_name As String
    
    old_name = ActiveWorkbook.Name
    new_name = Replace(old_name, "Q114", "Q214")
    
    ActiveWorkbook.SaveAs Filename:=new_name
    
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: Save with a new filename, replace existing string

    @Xx7....I guess you are assuming that the "document" to be renamed is only an excel document by using ActiveWorkbook.Name. If you have .pdf and other extensions, it will have a limitation. That is why I prefer to loop through the documents

  6. #6
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Re: Save with a new filename, replace existing string

    Xx7 - the formula didn't work.

    The files are still named Q114

  7. #7
    Forum Contributor
    Join Date
    03-09-2010
    Location
    Oslo, Norway
    MS-Off Ver
    Excel 2003
    Posts
    173

    Re: Save with a new filename, replace existing string

    Hello,

    Thank you for the replies,

    The names of the documents differ, but they all have Q114 in them. I guess Xx7's sollution is the best?

  8. #8
    Forum Contributor
    Join Date
    10-01-2013
    Location
    Houston
    MS-Off Ver
    Excel 2010
    Posts
    136

    Re: Save with a new filename, replace existing string

    Xx7 method will change only one file name (excel file). But if you have multiple names and multiple documents (.doc, .pdf etc) you can list them as I specified in Column A and B, and loop through the documents.

    Don’t forget to click the star if this helped you, and don’t forget to mark the thread as solved

+ 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. [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
  2. Save as dialog with predefined filename does not save
    By a94andwi in forum Excel General
    Replies: 2
    Last Post: 04-20-2012, 03:23 AM
  3. Using String as filename in Save As not recognised by Dir
    By NoCanDo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2008, 05:49 PM
  4. How to replace a filename by a variabel
    By Dibo in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-09-2006, 06:27 PM
  5. Replies: 0
    Last Post: 12-02-2005, 05:08 PM

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