Page 1

Forcing Users to Enable Macros In order to use any macro in Excel, your users must “Enable” macros. That is, they must give Excel permission to run macros in their environment. They can do this by clicking the “Enable Content” button on the Security Warning that pops up above the Formula Bar when the file is opened. The bottom line is that you can write all the fancy macros in the world, but they will never run unless users “Enable Macros.” This begs the question: Can you force users to enable macros? The answer is, of course, yes—with a little Excel trickery. The idea is relatively simple. You create a sheet named something like “START.” On that sheet, there is nothing more than just a simple warning stating that macros must be enabled like the sample below.

You then hide all sheets in the workbook except for the one labeled “START.” Finally, you write a simple macro that will unhide all the sheets when the workbook is opened. At this point, when the workbook is opened, Excel will ask the user to enable macros. The user isn’t given an option to not enable macros because all they will see is your “START” sheet. The other sheets will still be hidden!

How it Works: In order to make this work, you actually need two macros: one macro that hides all but the “START” sheet when the workbook closes and another macro that unhides all but the “START” sheet when the workbook opens.

978-1-118-28121-5 Wiley and the Wiley logo are registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries.


First, let’s tackle the actions that need to happen when the workbook closes. 1. Activate the Visual Basic Editor by pressing ALT+F11 on your keyboard 2. In the Project window, find your project/workbook name and click the plus sign next to it in order to see all the sheets 3. Click ThisWorkbook 4. Select the BeforeClose event in the Event drop-down menu

5. Type or paste this code: Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Step 1: Declare your variables Dim ws As Worksheet 'Step 2: Unhide the Starting Sheet Sheets("START").Visible = xlSheetVisible 'Step 3: Start looping through all worksheets For Each ws In ThisWorkbook.Worksheets 'Step 4: Check each worksheet name If ws.Name <> "START" Then 'Step 5: Hide the sheet ws.Visible = xlVeryHidden End If 'Step 6: Loop to next worksheet Next ws 'Step 7: Save the workbook ActiveWorkbook.Save End Sub

978-1-118-28121-5 Wiley and the Wiley logo are registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries.


Step 1: Declare an object called ws. This will create a memory container for each worksheet we loop through. Step 2: Ensure that the “START” sheet is visible. Step 3: Start the looping by telling Excel we want to evaluate all worksheets in this workbook. Step 4: Compare the name “START” to the sheet that is currently being looped to ensure that the actions that come next are applied to all sheets except the “START” sheet. Step 5: If the sheet names are different, hide the sheet using the xlVeryHidden property. This property not only hides the sheet, but prevents the user from manually unhiding it using the user interface. Step 6: Loop back to get the next sheet. Step 7: Once all sheets are evaluated, the macro saves the workbook and ends. Now, we’ll write a macro to handle all the actions that need to happen when the workbook opens. 1. Activate the Visual Basic Editor by pressing ALT+F11 on your keyboard 2. In the Project window, find your project/workbook name and click the plus sign next to it in order to see all the sheets 3. Click ThisWorkbook 4. Select the Open event in the Event dropdown menu

978-1-118-28121-5 Wiley and the Wiley logo are registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries.


5. Type or paste this code: Private Sub Workbook_Open() 'Step 1: Declare your variables Dim ws As Worksheet 'Step 2: Start looping through all worksheets For Each ws In ThisWorkbook.Worksheets 'Step 3: Unhide All Worksheets ws.Visible = xlSheetVisible 'Step 4: Loop to next worksheet Next ws 'Step 5: Hide the Start Sheet Sheets("START").Visible = xlVeryHidden End Sub

Step 1: Declare an object called ws. This will create a memory container for each worksheet we loop through. Step 2: Start the looping by telling Excel we want to evaluate all worksheets in this workbook. Step 3: Unhide the sheet that is currently being looped. This will in effect unhide worksheets as each sheet is made visible. Step 4: Loop back to get the next sheet. Step 5: Once all sheets are made visible, this step hides the “START” sheet. Again, we use the xlVeryHidden property so that the user will not be able to manually unhide it using the user interface. Once both macros are implemented, you will have a workbook that will only work if the user enables macros!

About the Authors: Michael Alexander is a Microsoft Excel MVP who has written several books on advanced business analysis with Access and Excel, including Excel Dashboards & Reports, which is also part of the Mr. Spreadsheet’s Bookshelf series. John Walkenbach, arguably the foremost authority on Excel, has written hundreds of articles and created the award-winning Power Utility Pak. His 50plus books include Excel 2010 Formulas, Excel 2010 Power Programming with VBA, and the bestselling Excel Bible.

978-1-118-28121-5 Wiley and the Wiley logo are registered trademarks of John Wiley & Sons, Inc. and/or its affiliates. Excel is a registered trademark of Microsoft Corporation in the United States and/or other countries.


101 Ready-To-Use Excel Macros  

sample chapter

Advertisement
Read more
Read more
Similar to
Popular now
Just for you