sorry for the incorrect title -- it should be How to rename FILE in VBA and not folder. it is not allowing me to change the title now.
In the above code, in each new folder a file is created with name given in variable strFile.
How do I create/rename files with this name in each folder? I want the file name to be this = ar(i, 1) & "-" & ar(i, 2)
VBA Code:
Sub createfolders()
fpath = "C:\Users\xx\Downloads\" 'path to create folders
ar = Sheets(1).Cells(1, 1).CurrentRegion
Dim strFile As String
strFile = "D:\Test VBA\XYZ.xlsx" 'File to copied
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim oFile As Object
Set oFile = fso.GetFile(strFile)
For i = 1 To UBound(ar)
c00 = fpath & ar(i, 1) & "-" & ar(i, 2)
If Dir(c00) = "" Then MkDir c00
If Not fso.FileExists(fso.BuildPath(c00, oFile.Name)) Then oFile.Copy fso.BuildPath(c00, oFile.Name)
Next
Set oFile = Nothing
Set fso = Nothing
End Sub
In the above code, in each new folder a file is created with name given in variable strFile.
How do I create/rename files with this name in each folder? I want the file name to be this = ar(i, 1) & "-" & ar(i, 2)
Last edited: