Rename files in directory from column B

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
I've seen this before but can't seem to locate it.

I would like to pull files from a directory into column A and then from column B I will rename those files and then I would like to rename the files from what is in column B.

Here is the code which I use to capture the file names in column A.

Code:
Sub ListFilesNames()
    Dim strTemp As String, MyPath As String, myFile As String
    Dim intCount As Integer, f()

    intCount = 0
    MyPath = ActiveWorkbook.Path & Application.PathSeparator
    myFile = "*.*"
    strTemp = Dir(MyPath & myFile)

    Do While strTemp <> Empty
        intCount = intCount + 1
        ReDim Preserve f(1 To intCount)
        f(intCount) = strTemp
        strTemp = Dir
    Loop

    With Sheets("Sheet1")
        .Range("A:A").Clear
        .Range("A1").Resize(UBound(f), 1).Value = WorksheetFunction.Transpose(f)
    End With
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
Sub Rename_Files()

    Dim cell As Range, MyPath As String

    MyPath = ActiveWorkbook.Path & Application.PathSeparator
    
    For Each cell In Range("A1", Range("A" & Rows.Count).End(xlUp))
        If cell.Offset(, 1) <> "" Then
            Name MyPath & cell.Value As MyPath & cell.Offset(, 1).Value
        End If
    Next cell

End Sub
 
Upvote 0
You're welcome Jeff. Glad it worked for you.
I found it failed with my case when the name lists in both column A and B are in Vietnamese language ( unicode ), the error notification is run-time error "52" bad file name or number".
How do I edit this script to make it works as all my file names are in Vietnamese language
 
Upvote 0
hi,

i try to get the names from my directory to colum a but i get

"run time error 1004"

and when i try to debug there is a problem with this line.

" .Range("A:A").Clear
.Range("A1").Resize(UBound(f), 1).Value = WorksheetFunction.Transpose(f)
"

can someone help?
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top