I am trying to close an Excel form that I developed using a button on the form. The debugger indicates the SaveAsUI as "variable not defined? I do not know what it wants me to provide. I did not write the code. Can someone help, please?
Option Explicit
Private Sub Workbook_BeforeSave(ByVal_SaveAsUI, Cancel)
Dim fname As Variant
On Error GoTo ErrorHandler
If **SaveAsUI** Then
Cancel = True 'Cancel the original SaveAs
'Get filename (with path) for saving
fname = Application.GetSaveAsFilename(fileFilter:="Excel Marcro-Enabled Workbook (\*.xlsm),\*.xlsm")
If fname = False Then Exit Sub 'Exit if user hit Cancel
Application.EnableEvents = False 'Prevent this event from firing
ThisWorkbook.SaveAs Filename:=fname, File Format:=52
'52 = xlOpenXMLWorkbookMacroEnabled = xlsm (with macro's in 2007-2010)
Application.EnableEvents = True 'Re-enable events
End If
Exit Sub
ErrorHandler:
Application.EnableEvents = True 'So events are never left disabled.
MsgBox "An error occured during save." & Err.Number, vbCritical, "Error"
End Sub