When closing the workbook, this event below is activated. If there are more that 20…
- User
When closing the workbook, this event below is activated. If there are more that 20 files in my backup folder, I get a message asking me if I want to delete the last files. When I click Yes, I get an error on the line 'Dim folder As Scripting.folder' in 'Function GetFilesSortedByDateCreated(FolderPath As String, ascendingOrder As Boolean) As Collection'.
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 = ThisWorkbook.Path & "\z SafetyChecksBckup"
backupPath = backupPath & "Safety Checks_" & Format(Now, "ddmmyy_hhmmss") & ".xlsb" Me.SaveCopyAs backupPath MsgBox "Backup Saved", vbInformation GoTo Over20 Else If response = vbNo Then GoTo WBClose Else If response = vbCancel Then Cancel = True Exit Sub End If End If End If Over20: ' Delete oldest backups if more than 20 backups exist If CountCloseBackupsInFolder(backupPath) >= 20 Then If MsgBox("The number of backups has exceeded 20. Do you want to delete the 10 earliest backups?", vbYesNo) = vbYes Then DeleteOldestBackups backupPath, 10 Else MsgBox "Surplus Files not Deleted", vbInformation Exit Sub End If End If ThisWorkbook.Save WBClose: End Sub
' Function to count the number of backups in a folder Function CountCloseBackupsInFolder(FolderPath As String) As Long Dim backupFiles As Object Dim backupFile As Object Dim count As Long FolderPath = ThisWorkbook.Path & "\z SafetyChecksBckup" Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(FolderPath).files For Each backupFile In backupFiles If UCase(Right(backupFile.Name, 5)) = ".XLSB" Then count = count + 1 End If Next backupFile CountCloseBackupsInFolder = count End Function
' Function to count the number of backups in a folder Function CountStartBackupsInFolder(FolderPath As String) As Long Dim backupFiles As Object Dim backupFile As Object Dim count As Long FolderPath = ThisWorkbook.Path & "\z SafetyChecksBckup" Set backupFiles = CreateObject("Scripting.FileSystemObject").GetFolder(FolderPath).files For Each backupFile In backupFiles If UCase(Right(backupFile.Name, 5)) = ".XLSB" Then count = count + 1 End If Next backupFile CountStartBackupsInFolder = 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 Collection Dim oldestBackupFiles As Collection Dim backupFile As Variant Dim i As Long ' Get all backup files sorted by date created in ascending order Set backupFiles = GetFilesSortedByDateCreated(FolderPath, True) ' Delete the oldest backup files, up to the specified count i = 0 Set oldestBackupFiles = New Collection ' Initialize the collection For Each backupFile In backupFiles If UCase(Right(backupFile, 5)) = ".XLSB" Then If i < count Then ' Add backup file to collection of oldest backup files oldestBackupFiles.Add backupFile End If i = i + 1 End If Next backupFile ' Delete the oldest backup files For Each backupFile In oldestBackupFiles Kill backupFile Next backupFile End Sub
- Assistant
- User
Thanks again.
- Assistant
You're welcome! If you have any more questions or need further assistance with your VBA code or anything else, feel free to ask. Happy coding!