Solved

Batch Rename Files from Excel using VBA

Posted on 2013-06-12
8
562 Views
Last Modified: 2013-06-14
Dear Experts :

I need to do some renaming by using an Excel makro

Column A: All files listed there are to be renamed. They are all located in C:\Test\
Column B: The new names are to be taken from Column B

I have attached a sample file for your convenience.

Help is much appreciated. thank you very much in advance.

Regards, Andreas

Batch-Renaming-Files.xls
0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 39242574
Use this macro:

Public Sub RenameFiles()

    Dim Cell As Range
   
    With ActiveSheet
        For Each Cell In Intersect(.Range("A:A"), .UsedRange).Cells
            If Cell.Value <> "" Then
                Name "C:\Test\" & Cell.Value As "C:\Test\" & Cell.Offset(0, 1).Value
            End If
        Next Cell
    End With

End Sub

Kevin
0
 

Author Comment

by:AndreasHermle
ID: 39242600
HI Kevin,

thank you very much for your quick help.

I am afraid to tell that your code throws an error message (Runtime Error 5) on ...

 Name "C:\Test\" & Cell.Value As "C:\Test\" & Cell.Offset(0, 1).Value

Any idea why?

regards, Andreas
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39242610
That's an access denied error. Do you have security rights to that folder? Is one of the files open?

Kevin
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 17

Accepted Solution

by:
andrewssd3 earned 250 total points
ID: 39242614
As coded this needs a reference to the Microsoft Scripting Runtime.

Public Sub RenameFiles()

    Dim fso As Scripting.FileSystemObject
    Dim vFiles As Variant
    Dim i As Long
    Dim oFile As Scripting.File
    Dim sFilePath As String
    
    Const cFILE_PATH As String = "C:\Temp"
    
    Set fso = New Scripting.FileSystemObject
    
    vFiles = ActiveSheet.Cells(1).CurrentRegion.Value
    
    For i = 1 To UBound(vFiles, 1)
    
        With fso
            sFilePath = .BuildPath(cFILE_PATH, vFiles(i, 1))
            If .FileExists(sFilePath) Then
                Set oFile = .GetFile(sFilePath)
                
                oFile.Name = vFiles(i, 2)
                
            End If
        End With
    
    Next i

End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39242617
Sorry - a lot has happened since I started typing that last solution! Must refresh more often
0
 

Author Comment

by:AndreasHermle
ID: 39242735
Dear both,

thank you very much for your great help.

I am off to bed now and will do the testing and troubleshooting tomorrow.

Again, thank you very much for help

Regards, andreas
0
 

Author Comment

by:AndreasHermle
ID: 39249613
Ok, both codes work just fine. Thank you very much for your great job.

I will award points now, I suggest splitting the points equally and then post another question to also include a folder picker in the codes.

Again, thank you very much for your professional and swift help.

Regards, Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39249615
Acutally I cannot say which code is better since both work fine but since the button next to the 'best solution' is a radio button I am not unable to uncheck it.

Thank you very much for your great support. I really appreciate it.

Regards, Andreas
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question