raphagcwill
New Member
- Joined
- Jan 12, 2016
- Messages
- 41
Hello guys,
Is there anybody out there who could help me to adjust the below a little bit?
I have multiple sheets on my workbook and my macro renames sheet 1 based on the values of some cells.
Then the file to be saved in the folder will have the same name as sheet 1
The name will not always be the same, but it might happens sometimes.
If it happens, I want the macro to add a version number to the file's name. A version number is necessary since I do not want to overwrite the old file.
I have found the perfect macro, but i have been struggling to get it to work flawlessly.
It is Worth saying that I VBA level is VERY basic.
From the beginning I had some simple saveas function
But after researching a lot i found the below macros that are supposed to be used together.
VBA Code To Save As A New Version If File Already Exists
I have changed the "test file path" to
On the macro below, I have modified the myfilename and Folderpath on the code below, but I am not sure what to change on mypath and SaveExt.
What happen sometimes is that the file is saved with the right extension (xlsm), but when I the error "Excel cannot open.....because the file of file extension is not valid"
On the folder the size of the file is only 8kb, but it should be over 205kb
Thanks in advance
Is there anybody out there who could help me to adjust the below a little bit?
I have multiple sheets on my workbook and my macro renames sheet 1 based on the values of some cells.
Then the file to be saved in the folder will have the same name as sheet 1
The name will not always be the same, but it might happens sometimes.
If it happens, I want the macro to add a version number to the file's name. A version number is necessary since I do not want to overwrite the old file.
I have found the perfect macro, but i have been struggling to get it to work flawlessly.
It is Worth saying that I VBA level is VERY basic.
From the beginning I had some simple saveas function
Code:
ActiveWorkbook.SaveAs "C:\Raphael\Raphael_" & Range("J2") & "_" & Range("L2").Value, 52, Password:="12345", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
But after researching a lot i found the below macros that are supposed to be used together.
VBA Code To Save As A New Version If File Already Exists
Code:
Function FileExist(FilePath As String) As Boolean
'PURPOSE: Test to see if a file exists or not
'SOURCE: www.TheSpreadsheetGuru.com/The-Code-Vault
'RESOURCE: http://www.rondebruin.nl/win/s9/win003.htm
Dim TestStr As String
'Test File Path (ie "C:\Users\Chris\Desktop\Test\book1.xlsm")
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
'Determine if File exists
If TestStr = "" Then
FileExist = False
Else
FileExist = True
End If
End Function
Code:
Sub SaveNewVersion_Excel()
'PURPOSE: Save file, if already exists add a new version indicator to filename
'SOURCE: www.TheSpreadsheetGuru.com/The-Code-Vault
Dim FolderPath As String
Dim myPath As String
Dim SaveName As String
Dim SaveExt As String
Dim VersionExt As String
Dim Saved As Boolean
Dim x As Long
TestStr = ""
Saved = False
x = 2
'Version Indicator (change to liking)
VersionExt = "_v"
'Pull info about file
On Error GoTo NotSavedYet
myPath = ActiveWorkbook.FullName
myFileName = Mid(myPath, InStrRev(myPath, "\") + 1, InStrRev(myPath, ".") - InStrRev(myPath, "\") - 1)
FolderPath = Left(myPath, InStrRev(myPath, "\"))
SaveExt = "." & Right(myPath, Len(myPath) - InStrRev(myPath, "."))
On Error GoTo 0
'Determine Base File Name
If InStr(1, myFileName, VersionExt) > 1 Then
myArray = Split(myFileName, VersionExt)
SaveName = myArray(0)
Else
SaveName = myFileName
End If
'Test to see if file name already exists
If FileExist(FolderPath & SaveName & SaveExt) = False Then
ActiveWorkbook.SaveAs FolderPath & SaveName & SaveExt
Exit Sub
End If
'Need a new version made
Do While Saved = False
If FileExist(FolderPath & SaveName & VersionExt & x & SaveExt) = False Then
ActiveWorkbook.SaveAs FolderPath & SaveName & VersionExt & x & SaveExt
Saved = True
Else
x = x + 1
End If
Loop
'New version saved
MsgBox "New file version saved (version " & x & ")"
Exit Sub
'Error Handler
NotSavedYet:
MsgBox "This file has not been initially saved. " & _
"Cannot save a new version!", vbCritical, "Not Saved To Computer"
End Sub
I have changed the "test file path" to
Code:
'Test File Path (ie "C:\Raphael
On the macro below, I have modified the myfilename and Folderpath on the code below, but I am not sure what to change on mypath and SaveExt.
What happen sometimes is that the file is saved with the right extension (xlsm), but when I the error "Excel cannot open.....because the file of file extension is not valid"
On the folder the size of the file is only 8kb, but it should be over 205kb
Code:
'Pull info about file
On Error GoTo NotSavedYet
myPath = ActiveWorkbook.FullName
myFileName = "Raphael_" & Range("J2") & "_" & Range("L2")
FolderPath = "C:\Raphael
SaveExt = "." & Right(myPath, Len(myPath) - InStrRev(myPath, "."))
On Error GoTo 0
Thanks in advance
Last edited: