Saving a timestamped file?

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
1,329
Office Version
  1. 2007
Platform
  1. Windows
Hi all,


I have some code as shown below, that allows me to regularly save an excel workbook with dates and time quickly.
The code works fine if the code is in each workbook, however I can see that it might be enhanced in the future and do not really want multiple copies in all my sheets.
So I pasted the code to my personal.xlsb and call it with


Rich (BB code):
Application.Run "Personal.xlsb!BackupFile" ', ActiveWorkbook.FullName


Before doing this I had to use in the local sub
Rich (BB code):
Application.Run "Personal.xlsb!Update_Display", "0"
as the Update_Display code was also in my Personal.xlsb
Rich (BB code):
Sub Update_Display(bSwitch As Boolean)
    Application.ScreenUpdating = bSwitch
    Application.EnableEvents = bSwitch
    Application.DisplayAlerts = bSwitch
End Sub



When I try calling the code in the Personal.xlsb I get error display 1004,stating 'File cannot be saved with the selected file type'

If I run it from the file itself, it works fine, so I *think* it is trying to save Personal.xlsb perhaps as an xlsm file? (not what I want anyway)

So how can I create a generic savefile routine to be called from anywhere?

Also this rasies a problem whereby, how do I code it so that another user could use the code?
Would I have to put an xlsb in some common location and refer explicitly to the path when using Application.Run method?

TIA






Rich (BB code):
Sub BackupFile(Optional pstrFile As String)
Dim strPfxDate As String, strPfxTime As String, strFullPath As String, strBackupPath As String, strWBname As String
Dim blnAlert As Boolean
' if no file supplied to program, use active workbook
    If pstrFile = "" Then
        pstrFile = ActiveWorkbook.FullName
    End If
    Update_Display (0)
    
' Now get the parameters for the filename




    strPfxDate = Format(Now(), "yyyymmdd")
    strPfxTime = Format(Now(), "hhmmss")
    strFullPath = Left(pstrFile, InStrRev(ActiveWorkbook.FullName, "\"))
    strWBname = Right(pstrFile, Len(pstrFile) - InStrRev(ActiveWorkbook.FullName, "\"))
    strBackupPath = strFullPath & "Backup\"
    ' If backup path does not exist, create it
    If Dir(strBackupPath, vbDirectory) = "" Then
        MkDir strBackupPath
    End If
    ' Switch off alerts if on
    
    'Save the file
    Application.StatusBar = "Saving Date and Timestamped file"
    ThisWorkbook.SaveAs Filename:=strBackupPath & strPfxDate & "_" & strPfxTime & "_" & strWBname
    Application.StatusBar = "Saving again as normal name"
    ThisWorkbook.SaveAs Filename:=strFullPath & strWBname
    Application.StatusBar = False
    Update_Display (1)
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,215,650
Messages
6,126,017
Members
449,280
Latest member
Miahr

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