Share via

Why is SaveAsUI indicated as "variable not defined"?

Anonymous
2024-11-05T18:44:47+00:00

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

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Andreas Killer 144.1K Reputation points Volunteer Moderator
2024-11-06T15:15:12+00:00

Gaye,

Whatever the point of this should be, it will never work that way.

Workbook_BeforeSave is an event routine (placed in the code module ThisWorkbook), that is called automatically from the application.

Therefore there is no need and it makes no sense to call it and, as the routine is declared Private, no way to call it from a regular module.

Andreas.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-11-06T15:06:12+00:00

    Hello, Andreas. Thank you for your help. Yes, the restructured header now works.

    However, when I try to Combine the macros for my Save As button, an error highlights "As" and indicates that there is an "Expected: list separator or )"

    I've been going in circles...when I change one thing, another error pops up. Your thoughts?

    Gaye

    Was this answer helpful?

    0 comments No comments
  2. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2024-11-06T11:22:10+00:00

    The header of the sub in invalid, there is an underscore between ByVal and SaveAsUI. And also the declaration of Cancel is missing:

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Andreas.

    Was this answer helpful?

    0 comments No comments