Renaming Workbooks

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Quick question, how do you rename all of the workbooks whose name contains
the word "Jul" in a certain folder by replacing the "Jul" with "August"?
 
Hi Anony

Make a copy of the folder to test it
You can create a loop through all files and use Name

Change the path to the folder in the code

'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"


Sub Example()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String, Fnum As Long

'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop


'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)

On Error Resume Next
Name MyPath & MyFiles(Fnum) As MyPath & _
Application.WorksheetFunction.Substitute(MyFiles(Fnum), "Jul", "August")
On Error GoTo 0

Next Fnum
End If


End Sub
 
Thank you!

Glad to be learning more VBA tricks by the day.
------
Cheers,
Anony


Ron de Bruin said:
Hi Anony

Make a copy of the folder to test it
You can create a loop through all files and use Name

Change the path to the folder in the code

'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"


Sub Example()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String, Fnum As Long

'Fill in the path\folder where the files are
MyPath = "C:\Users\Ron\test"

'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If

'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

'Fill the array(myFiles)with the list of Excel files in the folder
Fnum = 0
Do While FilesInPath <> ""
Fnum = Fnum + 1
ReDim Preserve MyFiles(1 To Fnum)
MyFiles(Fnum) = FilesInPath
FilesInPath = Dir()
Loop


'Loop through all files in the array(myFiles)
If Fnum > 0 Then
For Fnum = LBound(MyFiles) To UBound(MyFiles)

On Error Resume Next
Name MyPath & MyFiles(Fnum) As MyPath & _
Application.WorksheetFunction.Substitute(MyFiles(Fnum), "Jul", "August")
On Error GoTo 0

Next Fnum
End If


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Finding and Replacing Sheet Names 1
Need help with please.. 2
Find & Replace w/ name change 3
Find & Replace w/ Name Changes 3
autofilter 1
Date to Text conversion 10
forecasting 1
AutoFill the data 1

Back
Top