Warning when macros are not running

Tal

New Member
Joined
May 26, 2008
Messages
5
All,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
I have a complex excel that is being used by many users. The excel must not be used by users if the macros are not running on their computers (e.g. high protection level).<o:p></o:p>
<o:p></o:p>
I cannot assume the definitions are correct on all computers, so each time the file is being opened, I need to have this checked. If the macros are not running, I want to inform the user that he should fix it before using. <o:p></o:p>
<o:p></o:p>
I want the validation every time the screen is being opened.<o:p></o:p>
<o:p></o:p>
As you can see, the tricky part is to have something working when macros cannot run...<o:p></o:p>
<o:p></o:p>
I'll appreciate to hear your creative solution for this issue.<o:p></o:p>
<o:p></o:p>
Thanks, <o:p></o:p>
<o:p></o:p>
Tal<o:p></o:p>
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Tal, Try using this code in the "ThisWorkBook" Level of the Workbook that you want this feature to apply to. Basically this Macro will hide the sheets and display a warning message prompting the user to enable macros before the sheets will become visible. Hope this Helps :)

Code:
Private Sub workbook_beforeclose(Cancel As Boolean)
HideSheets
End Sub
 
Private Sub Workbook_Open()
UnhideSheets
End Sub
 
 Private Sub HideSheets()

Dim sht As Worksheet
 
Application.ScreenUpdating = False
Application.DisplayAlerts = False
ThisWorkbook.Sheets("Macros Disabled").Visible = xlSheetVisible
For Each sht In ThisWorkbook.Sheets
If sht.Name <> "Macros Disabled" And sht.Name <> "Master" And sht.Name <> "Template" Then sht.Delete
Next sht
For Each sht In ThisWorkbook.Sheets
If sht.Name <> "Macros Disabled" Then sht.Visible = xlSheetVeryHidden
Next sht

ThisWorkbook.Sheets("Macros Disabled").Protect Password:="Password"
Application.ScreenUpdating = True
ResetTemplate
ThisWorkbook.Save
End Sub
Private Sub UnhideSheets()
Dim sht As Object
Application.ScreenUpdating = False
For Each sht In ThisWorkbook.Sheets
sht.Visible = xlSheetVisible
Next sht
ThisWorkbook.Sheets("Macros Disabled").Visible = xlSheetVeryHidden
ThisWorkbook.Sheets("Template").Visible = xlSheetVeryHidden
ThisWorkbook.Sheets("Master").Protect Password:="password"
Application.ScreenUpdating = True
 
End Sub
 
Last edited by a moderator:
Upvote 0
Thank for the quick replay.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Can you please elaborate why you are suggesting to delete all sheets? <o:p></o:p>
If sht.Name <> "Macros Disabled" Then sht.Delete<o:p></o:p>
<o:p></o:p>
Is there a way to allow the question "do you want to save your changes?", so user will have the ability not to save their changes, and yet to do a manipulation using the workbook_beforeclose() that will be saved (I guess I'm looking for something like partial save)<o:p></o:p>
<o:p></o:p>
Thanks<o:p></o:p>
 
Last edited:
Upvote 0
Thank for the quick replay.<o:p></o:p>
<o:p></o:p>
Can you please elaborate why you are suggesting to delete all sheets? <o:p></o:p>
If sht.Name <> "Macros Disabled" Then sht.Delete<o:p></o:p>
<o:p></o:p>
Is there a way to allow the question "do you want to save your changes?", so user will have the ability not to save their changes, and yet to do a manipulation using the workbook_beforeclose() that will be saved (I guess I'm looking for something like partial save)<o:p></o:p>
<o:p></o:p>
Thanks<o:p></o:p>

Hi Tal, you can ignore that line you quoted above, i forgot to take it out of the code, because i have been using this code for a workbook i have, which basically resets the workbook to default after creating a whole array of sheets in it when it is being used. Anyways, in regard to your next question.... i'm not 100% sure what you are asking, so I will refer the question to someone else. Hope I've been able to help. Oh and one thing before I got, i forgot to mention in my post that in your workbook you needed to create a sheet called "Macros Disabled", with the instructions for enabling macros in it...this is what is shown if the user disables macros when they load the workbook...
 
Upvote 0
Tal,

see picture below:


2527171385_e0bbe2afb9_o.jpg
 
Upvote 0
Thanks ggounder. That's help a lot :)

I'm writting the general code, without the delete part. I'm sure it will help someone...
____________________________________________________

Code:
Private Sub workbook_beforeclose(Cancel As Boolean)
    HideSheets
End Sub
 
Private Sub Workbook_Open()
    UnhideSheets
End Sub
 
 Private Sub HideSheets()
    Dim sht As Worksheet
    
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    ThisWorkbook.Sheets("Macros Disabled").Visible = xlSheetVisible
    
    For Each sht In ThisWorkbook.Sheets
        If sht.Name <> "Macros Disabled" Then sht.Visible = xlSheetVeryHidden
    Next sht
    
    ThisWorkbook.Sheets("Macros Disabled").Protect Password:="password"
    Application.ScreenUpdating = True
    
    ThisWorkbook.Save
End Sub
 
Private Sub UnhideSheets()
    Dim sht As Object
    
    Application.ScreenUpdating = False
    
    For Each sht In ThisWorkbook.Sheets
        sht.Visible = xlSheetVisible
    Next sht
    
    ThisWorkbook.Sheets("Macros Disabled").Visible = xlSheetVeryHidden
    
    Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
Upvote 0
Hi all,

A couple of things to consider..the way I read the code, it would be possible for someone to save the workbook in a "unlocked" state and then reopen without enabling macros..with full access to the workbook. Example...I open the workbook and make some changes...I save the workbook without closing..I then close the workbook without saving changes. I can now open the saved workbook with access to all pages without enabling macros.

Second..I don't know how the workbook is used or if this would apply, but if the workbook is ever emailed...If someone emails the workbook as an attachment from within excel while the workbook is open the attachment would also be in a "unlocked" state and could be accessed without enable macros.

I have some code that addresses these issues, but it is on a different computer than the one that I am currently using.

I know this may seem trivial..but my experience has been if the holes are there someone at some point will inadventently find one and potentally mess things up.
 
Upvote 0
JasonC71, sounds like very good points. If you will be able to attach some code samples it will be great...

Thanks!

Tal
 
Upvote 0
OK..the following code handles the save issue..the code itself is not mine, just modified to work for me. I apologize in advance for not correctly posting the code, but after an hour of trying to get the VB html thing to work I have given up.

The Code:

Option Explicit

Const WelcomePage = "Macros"

Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to '" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
Call CustomSave
Case Is = vbNo
'Do not save
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If

'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
.Close savechanges:=False
Else
Application.EnableEvents = True
End If
End With
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False

'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True

'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub

Private Sub Workbook_Open()
'Unhide all worksheets
Application.ScreenUpdating = False
Call ShowAllSheets
Application.ScreenUpdating = True
End Sub

Private Sub CustomSave(Optional SaveAs As Boolean)
Dim ws As Worksheet, aWs As Worksheet, newFname As String
'Turn off screen flashing
Application.ScreenUpdating = False

'Record active worksheet
Set aWs = ActiveSheet

'Hide all sheets
Call HideAllSheets

'Save workbook directly or prompt for saveas filename
If SaveAs = True Then
newFname = Application.GetSaveAsFilename( _
fileFilter:="Excel Files (*.xls), *.xls")
If Not newFname = "False" Then ThisWorkbook.SaveAs newFname
Else
ThisWorkbook.Save
End If

'Restore file to where user was
Call ShowAllSheets
aWs.Activate

'Restore screen updates
Application.ScreenUpdating = True
End Sub

Private Sub HideAllSheets()
'Hide all worksheets except the macro welcome page
Dim ws As Worksheet

ThisWorkbook.Unprotect ("password")
Worksheets(WelcomePage).Visible = xlSheetVisible

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVeryHidden
Next ws


Worksheets(WelcomePage).Activate
Worksheets(WelcomePage).Range("A1").Select
ThisWorkbook.Protect ("password")
End Sub

Private Sub ShowAllSheets()
'Show all worksheets except the macro welcome page

Dim ws As Worksheet

ThisWorkbook.Unprotect ("password")

For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = WelcomePage Then ws.Visible = xlSheetVisible
Next ws

Worksheets(WelcomePage).Visible = xlSheetVeryHidden
Worksheets("HELLO").Activate
Worksheets("HELLO").Range("C2").Select
ThisWorkbook.Protect ("password")
End Sub

This works pretty well for me with one exception...If you are using excel 2007...If just the one workbook is open no problem, but if you have say two workbooks open excel sometimes locks up when you try to close one of the workbooks (but you can still save)...this does not happen in previous versions of excel as far as I know; just 2007. The code refers to a page called macros..this is the screen that a user sees if they do not enable macros.

I have to find the code for dealing with email....and if anyone knows why there is a problem with having two workbooks open in 2007 with the code advice would be appricated.

Have a good one....
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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