Solved

Batch Rename Files from Excel using VBA

Posted on 2013-06-12
8
526 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
Comment Utility
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
Comment Utility
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)
Comment Utility
That's an access denied error. Do you have security rights to that folder? Is one of the files open?

Kevin
0
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 250 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

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

Author Comment

by:AndreasHermle
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now