Make Users Enable Macros in Order to View a Workbook in Excel

Add to Favorites

Tutorial showing you how to make a user enable macros in a workbook in order to view the workbook.

This is very important when macros make-up a critical part of the workbook and not having them can stop the workbook from performing correctly or allow users to mess-up or corrupt the data and functionality of the spreadsheet.

There are a few steps involved in making this work securely and I will go through each of them below.

Steps to Force Users to Enable Macros in Order to View an Excel Workbook

  1. Create a worksheet that you want users to see if they have not enabled macros.
    86e7a9dc17e55e4a5e635c47dd25deb1.jpgIt is a good idea to include a message that tells the user what is going on.  I also removed the gridlines from this sheet just so that the message would be a bit more clear (View tab > then un-check Gridlines).
  2. Make sure you have at least one other worksheet tab.  This is where the actual data and anything else you want will go.  You can have as many worksheets as you want but I've used one here to make it easier to follow.
    4b2ec244291294bb661235c354a20495.jpg
  3. Hit Alt + F11 on the keyboard to go to the macro editor.
    d94b44565abb22ebc8136560e0bf8ad2.png
  4. Double-click where it says ThisWorkbook:
    e08707f611f6c2f720a51f2b6f2f8af8.jpg
  5. In the window that opens, copy and paste this code:
    
    

    Private Sub Workbook_Open()

    'worksheets to show when macro is enabled

    Sheets("Data").Visible = True

    'worksheet that shows reminder to enable macro

    Sheets("Reminder").Visible = xlVeryHidden

    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    'worksheet with reminder

    Sheets("Reminder").Visible = True

    'worksheets to show when macro is enabled

    Sheets("Data").Visible = xlVeryHidden

    ThisWorkbook.Save

    End Sub

    In the editor, it will look like this:

     e0d57d8756cc08093ddae7f60769ed05.jpg

    This code says that when the user opens the workbook, the Data worksheet will appear and the Reminder worksheet will disappear; but, when the user closes the workbook, the Reminder sheet will appear and the Data sheet will disappear and then the file will be saved, in order to save the hiding of the Data worksheet.

    If the user does not have macros turned-on, they will only ever see the Reminder worksheet and nothing else.

  6. Edit the macro to suit your needs.  Change Reminder to the name of the worksheet that will remind users to enable macros.

    Change Data to the name of the worksheet that will contain the data.  For the "Data" worksheet note that this will just be whatever worksheet you want the user to see once they have enabled macros. You can have as many of these as you need; simply copy/paste the line of code for the data worksheet below the other one and change "Data" to the name of the other worksheet.

    It would look something like this:
    8c86cb60c1a22e533aaed2c388a13de5.jpg

  7. Everything should now work, but we still need to secure our macro code so that a user can't break the functionality that we have just created and see the worksheets without having macros enabled - at this point, even just a semi-advanced user, or someone who did a five minute online search, could figure out how to show all of the hidden worksheets without having macros enabled.

    To secure the code, we need to right-click over the vba project, located on the left side of the window, and select VBAProject Properties...
    2a293b7975c88f1b0e2f0084638eb26a.jpg

  8. Go to the Protection tab and check where it says "Lock project for viewing" and enter a password for the project and then hit OK.
    16062032c41c0a4c59a17db58e83fc0a.png
  9. Now close the vba/macro editor window and then close the Excel workbook and save the file when it asks you to do so.  Make sure to save the file as a Macro-Enabled Workbook if you are in Excel 2007 and later or the macro won't work.

    That's it!

As long as the user has macros enabled, they will see the Data tab(s) when the workbook opens.

If they don't have macros enabled, they will only see the Reminder tab when the workbook opens.

Result:

4bf505f1d4c27feb23313d45fdb8a619.jpg

Once macros are enabled:

5071fefbf5440eecdf1a4733630b8463.jpg

Editing the Macro in the Future

Don't forget the password you used to lock the project.  When you go back into the VBA editor window and need to edit a macro, you will have to enter the password.

e746811b848466a0699c258b407fa0cb.png

Notes

This is the best way to make a user enable macros in order to use a workbook and it is something that I have used in the past in order to prevent users from messing-up important, and often very complex, workbooks that rely on macros in order to simplify everything.

This technique works even if the user is advanced in Excel because they cannot access the VBA or Macro code in order to edit the desired functionality of the workbook.

However, you should know that this does not encrypt the workbook.  Other programs might still easily be able to open this file and manipulate it if a user really wants the data inside.  Consider this technique as something to be used when you just want to prevent a user from breaking workbook functionality and corrupting data when they use a workbook without having macros enabled.

Sample Workbook

We included a sample Excel workbook with this tutorial so you can see how it works.  The code is also password protected.

The password is: 123456

(Never use that as your real password for Excel or anything else!!!  This is just to make it easier to use this sample workbook.)


Downloadable Files: Excel File

Similar Content on TeachExcel
Add a Drop Down Menu to a Cell in Excel - Data Validation Macro
Macro: Add a drop down menu or list to a cell in Excel with this free Excel macro. This is a...
Disable Calculation of Cells when Running a Macro in Excel
Tutorial: How to stop formulas and functions from updating in Excel when running a macro. This can s...
How to Create and Manage a Chart in Excel
Tutorial: In this tutorial I am going to introduce you to creating and managing charts in Excel. Bef...
How to Add Boxes, Buttons, Arrows, Speech-Bubbles, Hearts, and More to a Spreadsheet in Excel
Tutorial: In this tutorial I am going to cover inserting and editing Shapes in an Excel workbook, as...
Loop through a Range of Cells in a UDF in Excel
Tutorial: How to loop through a range of cells in a UDF, User Defined Function, in Excel. This is ...
Remove All Filtering From a Worksheet in Excel
Macro: This Excel macro removes all filtering from a worksheet in Excel. This allows you to ...