Can anyone here tweek this macro for renaming Excel files based on a cell's contents?

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
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
My IT guy says I have to know what macro code does before I can use it so that I am not running malicious code. I got someone to alter it so that I can put in the corporate calendar week into the filename. So I am trying to Google various parts of the code and I have found some useful explanations. Can you fill in the gaps? Here is what I found so far:

Code:
With Application.FileDialog(msoFileDialogFolderPicker)
selects a folder from a certain path and import all the files inside it http://stackoverflow.com/questions/1...cker-using-vba

Code:
filepath = .SelectedItems(1)
This part is involved in putting the filepath of the selected file in the variable filepath - See more at: http://lroedal.net/en/vba/read-from-....Od11itRI.dpuf or VBA: How to read from a text file to a string | lroedal.net

Code:
Set wb = Workbooks.Open
- The line that starts with this text uses the file path you selected in FileDialog - http://stackoverflow.com/questions/2...-manipulate-it

Code:
Sub RenameAllExcelFilesInFolderWInput()
With Application.FileDialog(msoFileDialogFolderPicker)     
[/INDENT]
<code>.Show     
filepath = .SelectedItems(1) 
End With  
Set r = Workbooks.Add.Worksheets(1).Range("A1") 
StrFile = Dir(filepath & "\*.*") 
sUser = InputBox("Enter Seven More Characters")  
Do While Len(StrFile) > 0     
strExtension = Split(StrFile, ".")(UBound(Split(StrFile, ".")))      
Set wb = Workbooks.Open(filepath & "\" & StrFile)     
StrNewfullfilename = wb.Sheets(1).Range("B1").Value & sUser & "." & 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 </code>  End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,803
Members
449,190
Latest member
cindykay

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