Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Force User to Enable Macros & Predefined Expiry Date

0

I have the following two VBA codes to: 1. Force user to enable macros & 2. Predefined expiry date. I am struggling to combine the two VBA's in order to function properly. VBA's as follow:

1. - Force User to enable Macros

Private Sub Workbook_BeforeClose(Cancel As Boolean)

' Runs after the user goes to close Excel but before any "Save" dialog

' may appear.

'

' Created by: TeachExcel.com

'

' Helper to show you when it runs.

'MsgBox "Close"

' Make the macro run faster by disabling screen updating

Application.ScreenUpdating = False

' Make sure no other events like this one run while we are working here

Application.EnableEvents = False

' Check if changes were made since the worksheets were made visible.

' Store the result into a variable.

' One line IF statement

If ThisWorkbook.Saved = True Then wbSaved = True

' Hide the worksheets and show the welcome screen.

Call Hide_Sheets

' Check if the workbook was saved before the sheets were hidden.

' This value is stored in the wbSaved variable before the worksheets

' are hidden because the process of hiding the worksheets will cause

' Excel to say that changes were made, even though the user himself

' didn't make any changes - all changed were as a result of hiding

' the worksheets and we don't want the user to have to see the Save

' dialog for this since it is not an expected behavior.

' (This can be confusing, I know!)

If wbSaved = True Then

    ' Save the workbook now since sheets were just hidden and we don't want the user

    ' to see them the next time they open this workbook unless they have macros enabled.

    ThisWorkbook.Save

End If

' Enable screen updating so Excel works like normal.

Application.ScreenUpdating = True

' Enable events again so everything works as expected

Application.EnableEvents = True

End Sub

Private Sub Workbook_Open()

' Runs when the workbook is opened or immediately after macros are enabled for the first time

' after the workbook is opened.

'

' Created by: TeachExcel.com

'

' Helper to show you when it runs.

'MsgBox "Open"

' Make the macro run faster by disabling screen updating

Application.ScreenUpdating = False

' Make sure no other events like this one run while we are working here

Application.EnableEvents = False

' Call the macro to show the worksheets

Call Show_Sheets

' Enable screen updating so Excel works like normal.

Application.ScreenUpdating = True

' Enable events again so everything works as expected

Application.EnableEvents = True

CheckExpiry

End Sub

Sub Show_Sheets()

' Show all worksheets except the splash screen

' Show worksheets before hiding the splash screen.

' - must always have at least 1 worksheet visible at any time

'

' Created by: TeachExcel.com

'

' Loop through all worksheets

For Each ws In Sheets

    ' Check if this is the welcome screen or not

    If ws.Name <> "Welcome" Then

        ' Show the worksheet

        ws.Visible = xlSheetVisible

    End If

Next ws

' Hide the welcome screen

Sheets("Welcome").Visible = xlSheetVeryHidden

' Set the workbook as "saved" even though the changes were not saved.

' - the "changes" were to unhide the worksheets and hide the welcome screen.

' - this allows us to not show the user the "Save" dialog if he closes the

'   workbook without actually changing anything in the worksheet.

ActiveWorkbook.Saved = True

End Sub

Sub Hide_Sheets()

' Hide all worksheets except the splash screen

' Show the splash screen before hiding the other worksheets.

' - must always have at least 1 worksheet visible at any time

'

' Created by: TeachExcel.com

'

' Show the welcome screen

Sheets("Welcome").Visible = xlSheetVisible

' Loop through all worksheets

For Each ws In Sheets

    ' Check if this is the welcome screen or not

    If ws.Name <> "Welcome" Then

        ' Hide the worksheet

        ws.Visible = xlSheetVeryHidden

    End If

Next ws

End Sub

2. Predefined Expiry Date

Sub CheckExpiry()

    Dim Expiry As Date

        Expiry = "31 December 2021"

    If Date > Expiry Then

        MsgBox "This file has expired. Please request the latest version!", vbCritical, "File will close"

        Application.DisplayAlerts = False

        ActiveWorkbook.Save

        Application.DisplayAlerts = True

        Application.Quit

    Else

        MsgBox "You have " & Expiry - Date & "Day(s) left", vbinfomation, "File expires on " & Expiry

    End If

End Sub

Answer
Discuss

Answers

0

Hi Duncan

Suggest you copy the "Predefined Expiry Date" macro called Private Sub Workbook_Open into a Module and rename it Sub CheckExpiry(). You also need to correct an error in that code- replace:

Application.Quiet
(which is not a valid method) with:
Application.Quit
which will close Excel once the message box is dismissed.

Then in the Force Macros sub.. Workbook_Open, add this line just before End Sub:

CheckExpiry
That macro will then run at the end of the open macro. 
Discuss

Discussion

Hi John

Thank you for your answer. I have done so accordinly.
Once I run the code, I get a Compile Error: Syntax Error.
If I remove the line "Select All", the code runs without an error.
Only other issue I have is that once I click on Close Excel and It asks whether to save or not and the user selects cancel, the only visible sheet after that is the welcome sheet?
duncanvz1 Sep 9, '21 at 11:38 am
Sorry Duncan was replying on my phone (from memory). I'll look on my PC later hopefully and revise my answer.

Could you edit your original question to use the Attach Files... button to attach your Excel file please? 
That will save me time and I can check for the source of the error you mention.
John_Ru (rep: 6142) Sep 9, '21 at 11:52 am
No need- I found the error and revised my answer to show you where to make a change. Regarding SelectAll, the explanation is below .. 
John_Ru (rep: 6142) Sep 9, '21 at 1:08 pm
BTW I don't think it's fair to describe Don's two macros as "simple" (in your original question) since they provide quite sophisticated functionality. Kindly edit your original question to remove that word.

Also please note that macros should be pasted with CODE tags so it can be copied into the clipboard by simply using the SelectAll button (there's a CODE button above each text box in the Forum)
John_Ru (rep: 6142) Sep 10, '21 at 12:45 am
Hi John
Apologies, this was my first time posting in the forum and well noted on all the suggestions/advice.
The VBA seems to be working fine now after the amendments and no errors are displayed. I am experiencing one issue though, and this only happens when I click on close workbook(top right corner) and then selcet cancel. Can I perhaps attached a copy of the spreadsheet in order for you to get a better understanding?
duncanvz1 Sep 10, '21 at 8:53 am
Hi John
I have made the necessary edits to my original post as well and I hope that I have done so correclty. 
duncanvz1 Sep 10, '21 at 9:01 am
Duncan

I've tried your file (with macros enabled) and the workbook just closes (as expected, with the Workbook_Close macro calling Hide_Sheets and saving the workbook ready for the next file opening). I can't help therefore but if you cancel and only see the Welcome sheet when you do it, the Hide_Sheets macro does that deliberately.

Thanks for removing the word "simple" from the question. Next time, please click the CODE button then paste your code where it is indicated (between the tags).

Finally, I think I answered your question (on how to combine the macros) so kindly mark the Answer as Selected (for the guidance of other users and for my reputation on the Forum). Thanks in advance.
John_Ru (rep: 6142) Sep 10, '21 at 9:38 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login