• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Rename all workbooks in a folder based on data from worksheets

naquin316

New Member
Members,


I have a huge folder with alot of sales tickets from my engineers. I have a system in place now on my computer to rename the workbooks after they are emailed to me. The code I am using is below. What I want to do now is rename all of the legacy workbooks by taking the data from 3 cells stringed together. Now some of the files are xlsx and xlsm. I want them to all be xls.

Code:


Sub SaveFile()

Dim NameFile As Variant

With Worksheets("SO1")

NameFile = .Range("M3") & "_" & .Range("C11") & "_" & .Range("B22") & ".xls"

End With

NameFile = Application.GetSaveAsFilename(InitialFileName:=Environ("USERPROFILE") & "DocumentsBaker Atlas VictoriaCased Hole Tickets" & NameFile, Filefilter:="Fichier Excel (*.xls), *.xls")

If NameFile = False Then

MsgBox "File not saved"

Else

ThisWorkbook.SaveAs Filename:=NameFile

End If

End Sub


I want the namefile variant to be the same, and the path " DocumentsBaker Atlas VictoriaCased Hole Tickets" to be the same too.

Can anybody help with this?
 
Hi, naquin316!


Give a look at this file:

https://dl.dropbox.com/u/60558749/Rename%20all%20workbooks%20in%20a%20folder%20based%20on%20data%20from%20worksheets%20%28for%20naquin316%20at%20chandoo.org%29.xlsm


You should place it in the same folder of the workbooks to be renamed, then open it an run RenameWorkbooksInFolder procedure. It's not tested so please check it before on a work folder with some files. But at least it compiles ok :)


I hope that if it's slightly non working you'd be able to tweak it as required. Otherwise just advise if any issue and please upload a compressed file with 5 workbooks so as to test it. Thank you.


Regards!


PS: Je pense que le expression "Fichier Excel" ne fonctionne pas bien du tout avec d'autres versions non anglaises d'Excel.

PS (translation): I think that the "Fichier Excel" expression won't work fully correct with other non-English Excel versions.


This is the code:

-----
Option Explicit

Sub RenameWorkbooksInFolder()
' constants
' this workbook
Const ksFilenamesWS = "Hoja1"
Const ksFilenamesRng = "FilenameList"
Const ksExcel = "*.xl*"
Const kiOriginal = 1
Const kiSaved = 2
Const kiExisting = 3
Const kiDeletable = 4
Const kiTroubleshooting = 5
Const ksOriginal = "Original"
Const ksSaved = "Saved"
Const ksExisting = "Existing"
Const ksDeletable = "Deletable"
Const ksTroubleshooting = "Troubleshooting"
' folder
Const ksWorksheet = "SO1"
Const ksCell1 = "M3"
Const ksCell2 = "C11"
Const ksCell3 = "B22"
Const ksUnderscore = "_"
Const ksDot = "."
' declarations
Dim rng As Range
Dim sFilename() As String, sFilenameNew As String
Dim iOriginal As Long, iSaved As Long, iExisting As Long, iDeletable As Long, iTroubleshooting As Long
Dim i As Long, A As String, bOk As Boolean
' start
' application
With Application
.ScreenUpdating = True 'False
.DisplayAlerts = False
End With
' range
Set rng = Worksheets(ksFilenamesWS).Range(ksFilenamesRng)
With rng
.ClearContents
.Cells(1, kiOriginal) = ksOriginal
.Cells(1, kiSaved) = ksSaved
.Cells(1, kiExisting) = ksExisting
.Cells(1, kiDeletable) = ksDeletable
.Cells(1, kiTroubleshooting) = ksTroubleshooting
End With
' initialize
iOriginal = 0
iSaved = 0
iExisting = 0
iDeletable = 0
iTroubleshooting = 0
' process
' existing files
A = Dir(ActiveWorkbook.Path & Application.PathSeparator & ksExcel)
Do While A <> ""
If A <> ThisWorkbook.Name Then
iOriginal = iOriginal + 1
ReDim Preserve sFilename(iOriginal)
sFilename(iOriginal) = A
End If
A = Dir
Loop
' new files
For i = 1 To iOriginal
' error trap on
On Error Resume Next
' open file
Workbooks.Open ActiveWorkbook.Path & Application.PathSeparator & sFilename(i)
If Err.Number > 0 Then
bOk = False
Else
bOk = True
' standardized name
With ActiveWorkbook.Worksheets(ksWorksheet)
sFilenameNew = .Range(ksCell1) & ksUnderscore & _
.Range(ksCell2) & ksUnderscore & _
.Range(ksCell3) & _
Right$(sFilename(i), InStr(StrReverse(sFilename(i)), ksDot))
If Err.Number > 0 And Err.Number <> 438 Then bOk = False
End With
' exists?
If bOk Then
A = Dir(ActiveWorkbook.Path & Application.PathSeparator & sFilenameNew)
If A = "" Then ActiveWorkbook.SaveAs ActiveWorkbook.Path & _
Application.PathSeparator & sFilenameNew
ActiveWorkbook.Close False
End If
DoEvents
End If
' summary
rng.Cells(i + 1, kiOriginal).Value = sFilename(i)
If bOk Then
If A = "" Then
rng.Cells(i + 1, kiSaved).Value = sFilenameNew
iSaved = iSaved + 1
rng.Cells(i + 1, kiDeletable).Value = sFilename(i)
iDeletable = iDeletable + 1
Else
rng.Cells(i + 1, kiExisting).Value = sFilenameNew
iExisting = iExisting + 1
End If
Else
rng.Cells(i + 1, kiTroubleshooting).Value = sFilename(i)
iTroubleshooting = iTroubleshooting + 1
End If
Next i
' end
' range
rng.Cells(1, 1).Select
Set rng = Nothing
' application
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
' terminate
MsgBox "Files read: " & iOriginal & vbCrLf & _
"Files saved: " & iSaved & vbCrLf & _
"Files previous: " & iExisting & vbCrLf & _
"Files deletable: " & iDeletable & vbCrLf & _
"Files troubleshooting: " & iTroubleshooting, _
vbApplicationModal + vbInformation + vbOKOnly, _
"Results"
Beep
End Sub
-----
 
No luck I was not able to get it to rename the files like I wanted it to.

Here are 5 files to test and the one you sent me. I appreciate your help.


https://www.dropbox.com/s/tlawrtkw7rejbnb/Test.zip
 
Hi, naquin316!

File updated, code edited in previous post, please download again from same link.

Regards!

PS: Once run Ok and checked you can build another macro to delete old files from column D. I think you can handle it; otherwise just advise.

PS2: Go to your default Excel files folder and clean all the garbage left there, I guess that all the saved files of the first try (with not fully correct names, indeed) might be there. Sorry for the inconvenience, it won't happen again... until next time :)
 
Thank you it worked great!! I want to add another range to the name of the newly created files. The problem is this cell is a date and shows up as 9/26/2012, well it wont work because of the character type. How can I add another cell, say "Z1", to the name and format it to be a date but in this format 9_26_2012.


Thanks!
 
Hi, naquin316!


Add this at the beginning of the procedure:

[pre]
Code:
-----
Const ksCell4 = "Z1"
-----
Change sFilenameNew definition to:

-----
sFilenameNew = .Range(ksCell1) & ksUnderscore & _
.Range(ksCell2) & ksUnderscore & _
.Range(ksCell3) & ksUnderscore & _
Format(.Range(ksCell4), "mm_dd_yyyy") & _
Right$(sFilename(i), InStr(StrReverse(sFilename(i)), ksDot))
-----
[/pre]
Adjust ksCell4 definition and date string format as required.


Regards!
 
Back
Top