Jon,
Is there a way to allow a user to open a workbook ONLY if they have allowed
the macros to execute?
Actually, no there isn't so you can stop looking.
One workaround is to make all sheets but one visible when saving the workbook
(rendeiring it useless) and on that sheet place a notice that macros need
to be enabled in order to use it. If macros are enabled when it's opened,
that notice sheet will hide itself and the other sheets will be made visible.
Attached below is some code and instructions on how to do this:
(Please make note that Excel is not a secure environment so if you're
using this to protect the workbook, it can be circumvented by an
experienced user)
John
<snip>
The only way to do this is to hide all the sheets except one when
the workbook is saved (making the workbook unusable).
On that one unhidden sheet, you'll need to place some text alerting
the user that they need to enable macros and instruction on how
to do so. You'll also need to protect the VBA project so they can't
easily get at the underlying code. Please be aware though that an
experienced user can crack any VBA code that you have or make
the sheets that you've hidden, visible again even without getting
into your VBA code.
Having said that, here's some code that will attempt to do what
you want. Try this in a brandy new workbook first to see how
it works and then adapt the code to fit your needs (based on how
your sheets are laid out)
How it works:
On Close, nothing happens (except the close) because the
stored copy already has all but sheet(1) hidden.
If the user saves the workbook...
His/her last active cell address is saved in a memvar
The sub to hide all but the first sheet is activated.
By default, this activates sheet(1) firing the code there.
There's a one second timer in the sheet activation code
(allowing the actual save to take place with all but the
first sheet hidden). After one second the OpenWorkbook
sub is called taking the user to the sheet he/she was last on.
The last cell on that sheet that was active is now selected.
'***** Workbook Code*****
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
' Remember where the user was
WhereWasI = ActiveCell.Address
' Call sub to hide all but the first sheet
HidealmostAll
End Sub
Private Sub Workbook_Open()
' Prevent Sheet1 Activate code from firing
Application.EnableEvents = False
' Call opening ceremonies sub
OpenWorkbook
Application.EnableEvents = True
End Sub
'***** Sheet(1) Code ******
Option Explicit
Private Sub Worksheet_Activate()
Application.OnTime Now + TimeValue("00:00:01"), "OpenWorkbook"
End Sub
'****Module1 Code *****
Option Explicit
Public WhereWasI As String
Sub HidealmostAll()
' Insure Sheet(1) is Visible
Sheets(1).Visible = True
Dim a As Integer
' Hide all the other sheets
For a = 2 To Sheets.Count
Sheets(a).Visible = xlVeryHidden
Next a
End Sub
Sub OpenWorkbook()
Dim a As Integer
' Display all the other sheets
For a = 2 To Sheets.Count
Sheets(a).Visible = True
Next a
Sheets(1).Visible = xlVeryHidden
If WhereWasI = "" Then Exit Sub
Range(WhereWasI).Activate
End Sub
<snip>