+ Reply to Thread
Results 1 to 4 of 4

Renaming the file name (or part of the file name) from a list

Hybrid View

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    2

    Renaming the file name (or part of the file name) from a list

    I need help with a VBA/macro that will rename multiple files from an Excel list (1) rename the entire file name (2) rename part of the file name.

    my path to the original file is C:\Temp\1234_customer#_$0.xlsx

    (1) Change the whole file name to 4321_customer#_120.xlsx
    (2) Change only part of the name from 1234 to 4321

    Attached file is an example of the list structure for both of my problems.

    Any help is greatly appreciated!
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Renaming the file name (or part of the file name) from a list

    this is an example, you can modify it for your goal
    Sub renamefile()
    
            Dim fs, f
            Set fs = CreateObject("Scripting.FileSystemObject")
            fpath = "D:\DATI\prova" ' <<<<<< to change
            Set f = fs.getfolder(fpath)
            Set NFile = f.Files
            For Each pf1 In NFile
                NomeFile = pf1.Name
                newname = ' <<<<<< here your code
                Name pf1 As fpath & "\" & newname
    
            Next
        End Sub
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    08-28-2012
    Location
    united states
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Renaming the file name (or part of the file name) from a list

    I am struggling to get this to work. I understand the fpath is the destination/renaming folder, but I am trying to reference column B on the attachment as the new file name. I am new to VBA so any help would be greatly appreciated- Thanks in advance!

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: Renaming the file name (or part of the file name) from a list

    Not sure about your list(2) though
    This is for List one.
    Sheet1("Rename entire file") must be active before you run the code
    Sub test()
        Dim r As Range, fn As String, msg As String
        Const myDir As String = "C:\Temp\"
        For Each r In Range("a2", Range("a" & Rows.Count).End(xlUp))
            fn = Dir(myDir & r.Value)
            If fn <> "" Then
                Name myDir & r.Value As myDir & r(, 2).Value
            Else
                msg = msg & vbLf & r.Value
            End If
        Next
        If Len(msg) Then MsgBox "Files not found" & msg
    End Sub

+ 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