Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Using Macros in Protected Workbooks.

Using Macros in Protected Workbooks

Written by Allen Wyatt (last updated April 20, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Lori had a problem with a workbook she wanted to share with others. The workbook contained a macro, but whenever the workbook is protected to prevent others from making changes to the workbook, Lori reports that "the macro is disabled."

Lori's exact problem is a little hard to reproduce, as testing shows that macros are still available in both protected worksheets and protected workbooks. You can still display the Macros dialog box and see the list of available macros. You can still choose one of the macros and run it.

Of course, seeing and running the macros may not be Lori's problem; it could be that the macro fails to run correctly when used on a protected worksheet. If that is the case, the problem typically only crops up if the macro is attempting to perform some action that violates the protection applied to the worksheet. For instance, if the protection doesn't allow for rows or columns to be deleted and the macro tries to do such, then it won't work.

The solution in this case is to modify your macro so that it unprotects the worksheet before making its changes. The following shows the basics of how this is done:

Sub ModifyProtectedSheet()
    ActiveSheet.Unprotect password:="yourpassword"

    'work on the worksheet here

    ActiveSheet.Protect password:="yourpassword", _
      DrawingObjects:=True, Contents:=True, Scenarios:=True
End Sub

The first line of this example unprotects the worksheet, you can then perform your processing, and then the last line again protects it. If your workbook uses protection, then the same technique can be used with the workbook—unprotect it, then make changes, then reprotect it.

Lori's problem could also be related to the word "sharing," which she used in her problem statement. If, by sharing, Lori means using Share Workbook to make the workbook "sharable" by others, then you will see a warning when sharing is activated. The warning indicates that macros cannot be "viewed or edited" in shared workbooks. This does not, however, mean that the macros are disabled, since you can still display the Macros dialog box to see a list of macros and choose one to run. You cannot, however, display the VBA Editor and look at the actual macro code.

Finally, there are some features of Excel that are simply disabled in shared workbooks. If your macro tries to perform any of these disabled actions, it won't work properly. This is a limitation of Excel, and there is nothing that can be done about it. (For more information on what cannot be done in a shared workbook, use the online help system and search for "shared workbooks, limitations.") The only way around these limitations is to not share the workbook.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12570) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Using Macros in Protected Workbooks.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Magnifying Only the Current Cell

You can use the Zoom feature of Excel to magnify what Excel shows of your workbook, but it affects the entire screen. ...

Discover More

Opening a Workbook and Suppressing Automatic Macros

Want to stop Excel from running any automatic macros that may be stored with a workbook? Here's how to do it.

Discover More

Transposing Table Contents

When you transpose information, it is essentially "rotated" in a direction. If you transpose the information in a table, ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Skipping Hidden Rows in a Macro

As your macro processes information in a worksheet, you may want to make sure that it skips over rows that are hidden. ...

Discover More

Calculating the Distance between the Top of the Window and Row 1

Normally Excel positions a UserForm in the center of your screen. You may want to position the form elsewhere, more ...

Discover More

Finding Other Instances of Excel in a Macro

When processing information using a macro, you may need to know if there are any other instances of Excel running on a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2024-04-20 11:21:51

J. Woolley

For related discussion:
https://excelribbon.tips.net/T010282_Using_a_Protected_Worksheet.html
https://excelribbon.tips.net/T009639_Visually_Showing_a_Protection_Status.html


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.