2

I have a macro in my Excel file that is meant to determine whether a user can open it or not. However, it seems like, depending on the settings of the user's machine, Excel by default disables the macro. This ended up opening and revealing the contents in the xls file.

How can I make sure that the user has to accept running the macro or Excel will close the workbook?

3
  • 3
    I don't see how. It would have to be without using VBA, since no VBA code will be run unless the user accepts running macros... I guess you can try to hide whatever is inside the file that you don't want to show and bring it up using the VBA code that runs when the macro is accepted? Jan 16, 2014 at 14:14
  • 3
  • This is awesome, @Jean-FrançoisCorbett! I wish I would have seen that before typing it out myself! Jan 16, 2014 at 16:36

2 Answers 2

5

UPDATED the code as it was not doing exactly what it was meant to...

This is pretty much the only way you can do this WITH a macro (thanks to @OlleSjögren for the insight :p)

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Dim Current As Worksheet
    Worksheets.Add(Before:=Worksheets(1)).Name = "Protection"
    Worksheets("Protection").Cells(1, 1) = "Please activate macros to view this workbook."

    For Each Current In Worksheets
        If Current.Name <> "Protection" Then
            Current.Visible = xlSheetVeryHidden
        End If
    Next Current


End Sub

Private Sub Workbook_Open()

    Dim Current As Worksheet

    For Each Current In Worksheets
         Current.Visible = xlSheetVisible
    Next Current

    Application.DisplayAlerts = False
    Worksheets("Protection").Delete
    Application.DisplayAlerts = True

End Sub

The property xlSheetVeryHidden means that it cannot be made visible through the UI (VBA code only can change it).

3
  • Thanks, I just tried this but it seems that clicking on the "Enable Macro" button doesn't trigger the Workbook_Open event because all the worksheets remain hidden.
    – Carven
    Jan 16, 2014 at 16:21
  • I see, I will see what I can do. Jan 16, 2014 at 16:24
  • Is the code placed inside the ThisWorkbook Excel Code Object? It's working just fine on my system; when I enable macros, it automatically runs the macro as if the workbook was just opened. Jan 16, 2014 at 16:33
0

I think you can't do it. But we can make a workaround.

  1. First of all you need to hide all your worksheet and prevent user to unhide it. Link. Following the link you will see that you can make a sheet veryHidden. As the link shows, you need to add a password to vba code too.

  2. Then you can to create a macro that adds an InputBox with password

    Sub CheckPassword()
        Dim password As String
    
        password = InputBox("Enter the password")  'You can use a Form with Textbox [PasswordChar] to put ****
        If password = "myPass" Then
            Sheets(3).Visible = xlSheetVisible
            Sheets(3).Select
        End If
    End Sub
    
  3. Finally you can attach a sub in the close event.

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Sheets(3).Visible = xlSheetVeryHidden
    End Sub
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.