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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

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,708
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,708
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)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,669
Messages
5,838,684
Members
430,563
Latest member
Raeyven

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