chrisd2000
New Member
- Joined
- Jun 23, 2014
- Messages
- 7
Can anyone here tweek this macro for renaming Excel files based on a cell's contents?
I use it to rename all the files in a folder based on the text that exists in cell B1.
Can you add some more code which would give me a prompt to enter 7 more characters to put right before .xls?
Here is the existing VBA code.
Sub RenameAllExcelFilesInDirectory()
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
filepath = .SelectedItems(1)
End With
Set r = Workbooks.Add.Worksheets(1).Range("A1")
StrFile = Dir(filepath & "\*.*")
Do While Len(StrFile) > 0
strExtension = Split(StrFile, ".")(UBound(Split(StrFile, ".")))
Set wb = Workbooks.Open(filepath & "\" & StrFile)
StrNewfullfilename = wb.Sheets(1).Range("B1").Value & "." & strExtension
wb.Close
r.Value = StrFile 'print old name
r.Offset(, 1).Value = StrNewfullfilename 'print new name
Set r = r.Offset(1)
Name filepath & "\" & StrFile As filepath & "\" & StrNewfullfilename
StrFile = Dir
Loop
End Sub
I use it to rename all the files in a folder based on the text that exists in cell B1.
Can you add some more code which would give me a prompt to enter 7 more characters to put right before .xls?
Here is the existing VBA code.
Sub RenameAllExcelFilesInDirectory()
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
filepath = .SelectedItems(1)
End With
Set r = Workbooks.Add.Worksheets(1).Range("A1")
StrFile = Dir(filepath & "\*.*")
Do While Len(StrFile) > 0
strExtension = Split(StrFile, ".")(UBound(Split(StrFile, ".")))
Set wb = Workbooks.Open(filepath & "\" & StrFile)
StrNewfullfilename = wb.Sheets(1).Range("B1").Value & "." & strExtension
wb.Close
r.Value = StrFile 'print old name
r.Offset(, 1).Value = StrNewfullfilename 'print new name
Set r = r.Offset(1)
Name filepath & "\" & StrFile As filepath & "\" & StrNewfullfilename
StrFile = Dir
Loop
End Sub