+ Reply to Thread
Results 1 to 2 of 2

Renaming files from Excel VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    06-27-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    1

    Renaming files from Excel VBA

    Hi Everyine,

    I have below code for renaming files , It only rename the file which is given in the path. I wan to rename files in subfolders. Please Help me out.

    Please [Sub rename_files()
    Dim v_Uq_Part As Variant, v_New_Name As Variant, tmp As Variant
    Dim o_FSO As Object, o_Fold As Object, o_File As Object
    Dim i As Long
    Dim s_Tmp As String, s_Ext As String
    
    
    Const S_PATH As String = "F:\testing"
    
    
    'set file scripting object
    Set o_FSO = CreateObject("scripting.FileSystemObject")
    
    
    'bring in data from sheet to 2D variant array
    v_Uq_Part = Range([a1], [a1].Offset(Cells(Rows.Count, 1).End(xlUp).Row - 1))
    v_New_Name = Range([b1], [b1].Offset(Cells(Rows.Count, 1).End(xlUp).Row - 1))
    
    
    'if last row in the second column is less then the first error
    If UBound(v_New_Name) < UBound(v_Uq_Part) Then
        MsgBox "Please enter new names for each existing part name"
        Exit Sub
    End If
    
    
    'check if path exists
    If Not o_FSO.folderexists(S_PATH) Then
        MsgBox "Input path is incorrect"
        Exit Sub
    End If
    
    
    'set folder object
    Set o_Fold = o_FSO.getfolder(S_PATH)
    
    
    'iterate through each file
    For Each o_File In o_Fold.Files
        s_Tmp = o_File.Name
        For i = 1 To UBound(v_Uq_Part)
            If InStr(1, s_Tmp, v_Uq_Part(i, 1)) > 0 Then
                If InStr(1, v_New_Name(i, 1), ".") = 0 Then s_Ext = "." & Split(s_Tmp, ".")(1) Else s_Ext = ""
                o_File.Name = v_New_Name(i, 1) & s_Ext
                Exit For
            End If
        Next i
    Next o_File
    
    
    End Sub]Login or Register [/url] to view this content.
    Last edited by mdrfan.ahmeds4; 09-12-2014 at 03:48 AM.

  2. #2
    Registered User
    Join Date
    11-13-2006
    Posts
    87

    Re: Renaming files from Excel VBA

    After renaming the files in the Folder, Loop thru the Folder.SubFolders and rename their files.

+ 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. Renaming Excel Files each month
    By EsKay! in forum Excel General
    Replies: 5
    Last Post: 07-22-2008, 07:08 PM
  2. Excel files replicating and renaming in a folder
    By hpum in forum Excel General
    Replies: 0
    Last Post: 07-21-2007, 09:23 AM
  3. Renaming files using excel
    By eINY in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-30-2007, 11:59 AM
  4. Renaming Non-Excel Files
    By QTGlennM in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2006, 03:55 PM
  5. need macro for renaming bunch of excel files
    By neowok in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-11-2005, 02:05 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