How to Rename Files In A Folder Using VBA??

liammoohan

Board Regular
Joined
Jan 11, 2008
Messages
72
I have a folder C:\Reporting\A10\

I have an excel sheet which when I run VBA will list all the files in the above folder in column 'A' (see below):

1647282572273.png


The values in column 'B' are what I need the files to be renamed to and was wondering if this can be done using VBA?

Thanks
Liam
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You could achieve this by using Dir and looping through the folder. Have a look at the link and you will be able to adapt to suit your needs. There are several other thread on the forum with similar query

Link
 
Upvote 0
A VBA demonstration for starters to paste only to the worksheet module :​
VBA Code:
Sub Demo1()
  Const C = ".csv", D = " - ", E = "*" & D, P = "C:\Reporting\A10\"
    Dim T$(), F$, R&, S$()
  ReDim T(1 To Rows.Count, 1)
        UsedRange.Clear
        F = Dir$(P & E & E & E & "*" & C)
  While F > ""
        R = R + 1
        S = Split(F, D)
        T(R, 0) = F
        T(R, 1) = Replace$(F, D & S(UBound(S)), C)
        Name P & F As P & T(R, 1)
        F = Dir$
  Wend
        If R Then Range("A1:B" & R).Value2 = T
End Sub
 
Upvote 0
How about

VBA Code:
Sub jec()
 Dim xFold As String, it As Variant
 xFold = "C:\Reporting\A10\"
 For Each it In Range("A1").CurrentRegion.Columns(1)
   Name xFold & it As xFold & it.Offset(, 1)
 Next
End Sub
 
Upvote 0
Didnt see, it’s the simplest way though?
 
Upvote 0

Yes, the reason why I felt into the trap …​
 
Upvote 0
How about

VBA Code:
Sub jec()
 Dim xFold As String, it As Variant
 xFold = "C:\Reporting\A10\"
 For Each it In Range("A1").CurrentRegion.Columns(1)
   Name xFold & it As xFold & it.Offset(, 1)
 Next
End Sub
Tried the code provided and it is Debugging (see image):
1647343027519.png
 
Upvote 0
A VBA demonstration for starters to paste only to the worksheet module :​
VBA Code:
Sub Demo1()
  Const C = ".csv", D = " - ", E = "*" & D, P = "C:\Reporting\A10\"
    Dim T$(), F$, R&, S$()
  ReDim T(1 To Rows.Count, 1)
        UsedRange.Clear
        F = Dir$(P & E & E & E & "*" & C)
  While F > ""
        R = R + 1
        S = Split(F, D)
        T(R, 0) = F
        T(R, 1) = Replace$(F, D & S(UBound(S)), C)
        Name P & F As P & T(R, 1)
        F = Dir$
  Wend
        If R Then Range("A1:B" & R).Value2 = T
End Sub
Tried the code provided and it is Debugging (see image):
1647343202523.png
 
Upvote 0

Forum statistics

Threads
1,215,437
Messages
6,124,871
Members
449,192
Latest member
MoonDancer

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