Solved

Batch Rename Files from Excel using VBA

Posted on 2013-06-12
8
545 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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