Disable X on workbook opened by macro

mwc0914

New Member
Joined
Sep 24, 2015
Messages
42
My module opens a xlsx workbook that can be updated.
I want to disable the Close X on the opened workbook so the user must use a userform to close & save the opened workbook.
How do I disable the X on the opened workbook from my original module?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
A simpler alternative to using the rather complex windows API approach is to remotely hook the close event of the newly opened workbook.

Something like this :

Code in the userform module.

Code:
Option Explicit

Private WithEvents wb As Workbook

[B][COLOR=#008000]'Button to open the new workbook[/COLOR][/B]
Private Sub CommandButton1_Click()
    Me.Tag = ""
    Set wb = Workbooks.Add
End Sub

[B][COLOR=#008000]'Button to close the opened workbook[/COLOR][/B]
Private Sub CommandButton2_Click()
    Me.Tag = "dummy"
    wb.Close
End Sub

Private Sub wb_BeforeClose(Cancel As Boolean)
    Cancel = Me.Tag = ""
End Sub
 
Last edited:
Upvote 0
Jaafar Tribak

If the UserForm is set to Non-Modal and with or without your macro ... the user can still use the main window X / Close Button to exit the workbook.

If the UserForm is set to Modal and with or without your macro ... the user cannot use the main window X / Close Button to exit the workbook.

The macro doesn't seem to have any effect .... ???
 
Upvote 0
Jaafar Tribak

If the UserForm is set to Non-Modal and with or without your macro ... the user can still use the main window X / Close Button to exit the workbook.

If the UserForm is set to Modal and with or without your macro ... the user cannot use the main window X / Close Button to exit the workbook.

The macro doesn't seem to have any effect .... ???

Hi,

If the UserForm is set to Non-Modal the code works well for preventing exiting the programmatically opened workbook (not the calling workbook)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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