How to run a macro when using the red X button before closing an excel sheet

BenKenya

New Member
Joined
Jul 23, 2020
Messages
9
Office Version
  1. 365
Platform
  1. MacOS
Hi there, I am trying to create a welcome page to my WorkBook and set a password activated button to only open specific sheets relevant to that one person. I think I have done this. But when they leave the spreadsheet via the RED X Button, all the sheets are left exposed. I want to set it up in a way that the RED X button when pressed to close activates a "Before -Close" function and hides all the sheet tabs and returns the Workbook back to the welcome sheet when opened by another member of our group. Is that even possible??

Thank you

Ben
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I am still searching for a way to do this, but I am trying to find a different way maybe using Command S to activate the same Macro. Any ideas folks would be amazing!

Thanks
 
Upvote 0
Welcome to the Forum BenKenya,

Place this code behind the workbook event for Before Close. In VBA double click ThisWorkbook, change the dropdown on the left from General to WorkBook and then on the right change the drop down to BeforeClose

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Dim StrName As String
    StrName = "Welcome"
    For Each ws In Worksheets
        If ws.Name <> StrName Then
            ws.Visible = xlSheetHidden
        End If
    Next
End Sub
 
Upvote 0
Trevor that is totally amazing!!

Magic

Thank you very very much
 
Upvote 0
Happy to help Ben, thank you for letting me know it has provided a solution for you. (y)
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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