Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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 December 7, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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, and Excel in Microsoft 365. 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

What Line Am I On?

At the bottom of your document, on the status bar, you can see the line on which your insertion point is located. It is ...

Discover More

Understanding the While...Wend Structure

One of the basic programming structures used in VBA is the While ... Wend structure. This structure helps to make the ...

Discover More

Changing the Outline Color of the Selected Cell

It can be a bit difficult, at times, to locate the selected cell on the screen. If you have difficulties in this area, ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Using Named Ranges in a Macro

Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...

Discover More

Maximum Length Limit for a Macro

Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros ...

Discover More

Clearing the Clipboard in a Macro

You may want your macro to clear the clipboard so that people cannot access anything left in the clipboard. That is ...

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 seven more than 1?

2018-09-18 12:05:40

Matt Salas

Hello,

I am using the following macro to allow uses to select multiple items from a drop-down list in the same cell. Cell I3 is where I have the data validation. However, it doesn't work if the sheet is protected. How can I incorporate this into the existing macro?

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To Select Multiple Items from a Drop Down List in Excel
Dim Oldvalue As String
Dim Newvalue As String




Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Address = "$I$3" Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub


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.