I have a VBA event that backs up my workbook to a folder when I close my workbook. Is…
- User
I have a VBA event that backs up my workbook to a folder when I close my workbook. Is there any way that the folder can be examined before the backup and if the nuber of backups exceed more than 20 individual backups, the 10 earliest backups are deleted with a Yes No pop up to warn that the delete procedure will take place. This is the Backup Event:
Private Sub Workbook_BeforeClose(Cancel As Boolean) Dim response As VbMsgBoxResult Dim backupPath As String Dim popup As Object Dim message As String Dim duration As Integer response = MsgBox("Do you want to save changes?", vbYesNoCancel) If response = vbYes Then backupPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup" 'backupPath = backupPath & "Backup_" & Format(Now, "yyyymmdd_hhmmss") & ".xlsm" backupPath = backupPath & "Service Providers_" & Format(Now, "ddmmyy_hhmmss") & ".xlsm" Me.SaveCopyAs backupPath message = "A secondary backup is being created" & vbNewLine & _ "" & vbNewLine & _ "PLEASE MONITOR DAILY BACKUP FOLDER SIZE" duration = 2 ' Time in seconds Set popup = CreateObject("WScript.Shell") popup.popup message, duration, "BACKUP", 64 ' 64 = Information icon ' Release the popup object Set popup = Nothing ThisWorkbook.Save ElseIf response = vbNo Then ThisWorkbook.Saved = True ElseIf response = vbCancel Then Cancel = True End If End Sub
- Assistant
- User
I am getting an error 'Path Not Found' in the line 'Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(folderPath).files' within the function below. ' Function to count the number of backups in a folder Function CountBackupsInFolder(folderPath As String) As Long Dim backupFiles As Object Dim backupFile As Object Dim count As Long Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(folderPath).files For Each backupFile In backupFiles If UCase(Right(backupFile.Name, 5)) = ".XLSM" Then count = count + 1 End If Next backupFile CountBackupsInFolder = count End Function
- Assistant
The "Path Not Found" error means that the folder path you provided for the backup folder is incorrect or does not exist. Please double-check the folder path and make sure it is valid.
Also, ensure that you have the necessary permissions to access the folder and its files.
If the folder path is correct and you still encounter the error, you can try adding error handling to gracefully handle the situation:
Function CountBackupsInFolder(folderPath As String) As Long Dim backupFiles As Object Dim backupFile As Object Dim count As Long On Error Resume Next Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(folderPath).Files On Error GoTo 0 If backupFiles Is Nothing Then ' Folder not found or empty CountBackupsInFolder = 0 Exit Function End If For Each backupFile In backupFiles If UCase(Right(backupFile.Name, 5)) = ".XLSM" Then count = count + 1 End If Next backupFile CountBackupsInFolder = count End FunctionThis revised code includes error handling that will set the count to 0 if the folder is not found or empty.
- User
Is it possible to include the actual backup path in the both the Function and the Event below: backupPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup"
' Function to count the number of backups in a folder Function CountBackupsInFolder(folderPath As String) As Long Dim backupFiles As Object Dim backupFile As Object Dim count As Long
Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(folderPath).files
For Each backupFile In backupFiles If UCase(Right(backupFile.Name, 5)) = ".XLSM" Then count = count + 1 End If Next backupFile
CountBackupsInFolder = count End Function
' Sub to delete the oldest backups in a folder, up to a specified count Sub DeleteOldestBackups(folderPath As String, count As Long) Dim backupFiles As Object Dim backupFile As Object Dim oldestBackupFile As Object Dim i As Long
' Get all backup files in the folder Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(folderPath).files
' Sort backup files by date created in ascending order backupFiles.SortOn "DateCreated", 1 ' 1 = Ascending
' Delete the oldest backup files, up to the specified count For Each backupFile In backupFiles If UCase(Right(backupFile.Name, 5)) = ".XLSM" Then backupFile.Delete i = i + 1 If i >= count Then Exit For ' Exit loop if specified count is reached End If Next backupFile End Sub
- Assistant
- User
Error: object does not support this method or function in the line 'backupFiles.SortOn "DateCreated", 1 ' 1 = Ascending'
- Assistant
- User
I am getting an error -Type Mismatch on the line ' Set files = folder.files ' in the function;' Function to get files sorted by date created in a folder Function GetFilesSortedByDateCreated(folderPath As String, ascendingOrder As Boolean) As Collection Dim fs As Object ' Scripting.FileSystemObject Dim folder As Object ' Scripting.Folder Dim file As Object ' Scripting.File Dim files As Collection Dim sortedFiles As Collection Dim fileDateCreated As Date Dim i As Long
folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup\" Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(folderPath) Set files = folder.files ' Add file paths to collection Set sortedFiles = New Collection For Each file In files sortedFiles.Add file.path Next file ' Sort collection based on file date created For i = 1 To sortedFiles.count - 1 For j = i To sortedFiles.count If (ascendingOrder And FileDateTime(sortedFiles(j)) < FileDateTime(sortedFiles(i))) Or _ (Not ascendingOrder And FileDateTime(sortedFiles(j)) > FileDateTime(sortedFiles(i))) Then sortedFiles.Add sortedFiles(i), Before:=j sortedFiles.Remove i End If Next j Next i Set GetFilesSortedByDateCreated = sortedFiles - Assistant
The "Type Mismatch" error occurs because the
folderobject is not being set correctly as aScripting.Folderobject. Instead, it is being declared as a genericObjecttype.To fix this issue, you can modify the code to explicitly declare
folderas aScripting.Folderobject. Here's the updated code:' Function to get files sorted by date created in a folder Function GetFilesSortedByDateCreated(folderPath As String, ascendingOrder As Boolean) As Collection Dim fs As Object ' Scripting.FileSystemObject Dim folder As Scripting.Folder Dim file As Object ' Scripting.File Dim files As Collection Dim sortedFiles As Collection Dim fileDateCreated As Date Dim i As Long folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zz ServProvDailyBckup\" Set fs = CreateObject("Scripting.FileSystemObject") Set folder = fs.GetFolder(folderPath) Set files = folder.Files ' Add file paths to collection Set sortedFiles = New Collection For Each file In files sortedFiles.Add file.Path Next file ' Sort collection based on file date created For i = 1 To sortedFiles.Count - 1