Solved

Batch Rename Files from Excel using VBA

Posted on 2013-06-12
8
565 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

617 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