Can anyone
HELP |
|
please?
This does everything it should - except - if you click 'Save' after showing the userform it gives the dreaded "This Program Has Performed an Illegal...." and is closed down
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim MyFilePath$
MyFilePath = "C:/My Documents/Backups for " & ActiveWorkbook.Name
On Error Resume Next '< already got a folder
MkDir MyFilePath
'save backup copy
ActiveWorkbook.SaveCopyAs Filename:=MyFilePath & "/" & _
(Format(Now(), "dd mmm yy h mm ss") & _
" " & ActiveWorkbook.Name)
End Sub[/vba]
[vba]Option Explicit
'<< CODE FOR USERFORM >>
Private Sub UserForm_Activate()
Dim N%, MyList(20, 0)
With Application.FileSearch
.LookIn = "C:/My Documents/Backups for " & ActiveWorkbook.Name
.Filename = "*.*"
If .Execute > 0 Then
For N = 1 To .FoundFiles.Count
MyList(N - 1, 0) = .FoundFiles(N)
Next N
End If
End With
ListBox1.List = MyList
End Sub
'delete all the backups and the folder
Private Sub CommandButton1_Click()
Dim N%
With Application.FileSearch
.LookIn = "C:/My Documents/Backups for " & ActiveWorkbook.Name
.Filename = "*.*"
If .Execute > 0 Then
For N = 1 To .FoundFiles.Count
Kill .FoundFiles(N)
Next N
End If
End With
'now delete the folder (folder must be empty)
RmDir "C:/My Documents/Backups for " & ActiveWorkbook.Name
're-set the list & view it
UserForm_Activate
End Sub
'purge all the backups except the last one
Private Sub CommandButton3_Click()
Dim N%
With Application.FileSearch
.LookIn = "C:/My Documents/Backups for " & ActiveWorkbook.Name
.Filename = "*.*"
If .Execute > 0 Then
For N = 1 To .FoundFiles.Count - 1
Kill .FoundFiles(N)
Next N
End If
End With
're-set the list & view it
UserForm_Activate
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub