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
 

Some videos you may like

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

BenKenya

New Member
Joined
Jul 23, 2020
Messages
9
Office Version
  1. 365
Platform
  1. MacOS
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
 

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,701
Office Version
  1. 2016
Platform
  1. Windows
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
 

BenKenya

New Member
Joined
Jul 23, 2020
Messages
9
Office Version
  1. 365
Platform
  1. MacOS
Trevor that is totally amazing!!

Magic

Thank you very very much
 

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,701
Office Version
  1. 2016
Platform
  1. Windows
Happy to help Ben, thank you for letting me know it has provided a solution for you. (y)
 

Watch MrExcel Video

Forum statistics

Threads
1,122,437
Messages
5,596,112
Members
414,042
Latest member
Swiftkoala

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
Top