How to call Workbook_BeforeClose Event when commandbutton is pressed

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Code:
Option Explicit

Private Sub CommandButton1_Click()
    Workbook_BeforeClose
End Sub


Private Sub UserForm_Initialize()
    Me.Caption = "Print, Save and Close"
    With Me.Label1
        .Caption = "Do you want to print, save and close the workbook named: " & """" & ActiveWorkbook.Name & """" & "?"
        .Font = "Tahoma"
        .Font.Size = "14"
    End With
    Me.CommandButton1.Caption = "Yes"
    Me.CommandButton2.Caption = "No"
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = 0 Then
        Cancel = True
        MsgBox "The X is disbled, please use a button on the form", vbCritical, "X is Disabled"
    End If
End Sub

At this point in time the code below is still unfinished until I can figure out how to call the workbook_BeforeClose Event
Workbook_BeforeClose Code Below
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim sFilePath As String
Dim sFileName As String




sFilePath = "C:\Users\UserNamer\Desktop\CompanyFolder\Project\Yield_Report\FileStorage\"
sFileName = Range("C11")


End Sub
When I run this code and click commandbutton1, then I get a "Compile error: Sub or Function not defined" with "Workbook_BeforeClose" highlighted in blue.

Thank you
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
The reason you're getting that error is that Workbook_BeforeClose is marked Private, so it's inaccessible from outside ThisWorkbook. You can easily get around this by simply creating a new procedure and placing the code that's in that event handler in this new one. Declare this new procedure as Public, and place it in a regular module. Then have both Workbook_BeforeClose and CommandButton1_Click call the new procedure.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top