Avatar of AndresHernando
AndresHernando

asked on 

Force macros while incorporating previous functionality

I want to have code that forces the user to enable macros, while still incorporating the previous functionality that occurs on open and close. I got this code (seen directly below) from Expert’s Exchange and tried to run it on an empty workbook and it seemed to work.

========================================================

Option Explicit

Dim blnClosing As Boolean

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    blnClosing = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Worksheets("Sheet1").Visible = True
    Worksheets("Sheet2").Visible = xlVeryHidden
    Worksheets("Sheet1").Activate
    If Not blnClosing Then
        Application.OnTime EarliestTime:=Now + TimeValue("00:00:01"), Procedure:="ThisWorkbook.ShowCorrect", Schedule:=True
    End If
End Sub

Private Sub Workbook_Open()
    blnClosing = False
    Worksheets("Sheet1").Visible = xlVeryHidden
    Worksheets("Sheet2").Visible = True
    Worksheets("Sheet2").Activate
End Sub

Public Sub ShowCorrect()
    Call Workbook_Open
End Sub

========================================================

So I adapted it to my application, and seemed to work again. The problem was that after saving and closing it a couple of times, it will then open without forcing me to enable macros.
I am trying to use the above code to force the user to enable macros, while incorporating the functionality of the code seen below. Sheet 1 above would become Sheet 4 (the mostly empty Macro Prompt sheet), while I would want Sheets 1, 2 and 3 from below (the sheets displaying the data) to be used in the same way that Sheet 2 from above is used (to be visible only when macros are enabled).

========================================================

Public Sub Workbook_Open()
       
    ThisWorkbook.Protect Password:="locked"
     
    Sheet1.EnableAutoFilter = True

    Sheet1.Protect Password:="locked", _
    Contents:=True, UserInterfaceOnly:=True
   
    Sheet3.Protect Password:="locked"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
                                           
    ThisWorkbook.Unprotect Password:="locked"
   
    Sheet1.Protect Password:="locked", _
    Contents:=True, UserInterfaceOnly:=True
   
    Sheet3.Protect Password:="locked"
   
    Sheet2.Visible = xlSheetHidden
End Sub

========================================================

I don’t know if the problem is between the version I’m using (Excel 2010) and previous versions, but even if I just used the stripped down code on an empty workbook, it still only works for only 1 or 2 openings. I even tried other code I found to do this same function (http://www.vbaexpress.com/kb/getarticle.php?kb_id=578), and I got the same results. At this point I’m spinning my wheels and would appreciate any help.

Thanks
Microsoft Excel

Avatar of undefined
Last Comment
Rob Brockett

8/22/2022 - Mon