ActiveWorkbook.Protect issues/questions

Afro_Cookie

Board Regular
Joined
Mar 17, 2020
Messages
103
Office Version
  1. 365
Platform
  1. Windows
Part of my Macro is protecting my workbook. If I run the same macro, with the workbook protected, it will unprotect my workbook even though there is nothing in the code telling it to do so.

Is this a common thing with protecting workbooks in VBA?

Below is a sampling of the my code.
VBA Code:
    Worksheets("Dashboard").Visible = False
       
    ActiveWorkbook.Protect "password"

    ActiveWorkbook.Save
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
is there a worksheet event that is unprotecting it?
 
Upvote 0
Not in this code there isn't. I have a separate macro that unprotects and unhides a bunch of stuff, but they don't interact with each other.

Complete code
VBA Code:
Sub Sort()
' Sort based on date, then inspector, start time of inspection
On Error Resume Next

    ActiveWorkbook.Worksheets("Production 2020").ListObjects("test").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Production 2020").ListObjects("test").Sort.SortFields.Add Key:=Range("test[DATE]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Production 2020").ListObjects("test").Sort.SortFields.Add Key:=Range("test[SHIFT]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Production 2020").ListObjects("test").Sort.SortFields.Add Key:=Range("test[QC/Insp.]"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
    
    With ActiveWorkbook.Worksheets("Production 2020").ListObjects("test").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    Call F0rmat 'Format
           
    Columns("P:AA").EntireColumn.Hidden = True
       
    Worksheets("Dashboard").Visible = False
       
    ActiveWorkbook.Protect "password"
            
    Call Last_Empty 'Empty_cell
    ActiveWorkbook.Save
    
End Sub
 
Upvote 0
check the worksheet module that you are working on, are there any codes in there?
 
Upvote 0
Only modules that I created. There is no code in any of the sheets or ThisWorkbook.

It doesn't affect functionality, but it removes the purpose of my protecting the workbook.

check the worksheet module that you are working on, are there any codes in there?
 
Upvote 0
It appears to be a kind of bug in Excel 2013, but it can be circumvented.
Replace this line
ActiveWorkbook.Protect "password"
with these lines
VBA Code:
With ActiveWorkbook
    If .ProtectStructure Then
        ' do nothing
    Else
        .Protect "Your_Password"
        .Save
    End If
End With
 
Upvote 0
Perfect! Thanks for the help.

It appears to be a kind of bug in Excel 2013, but it can be circumvented.
Replace this line
ActiveWorkbook.Protect "password"
with these lines
VBA Code:
With ActiveWorkbook
    If .ProtectStructure Then
        ' do nothing
    Else
        .Protect "Your_Password"
        .Save
    End If
End With
 
Upvote 0
You're welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top