Code locks open files, not just one being opened

Sharon in Denver

Board Regular
Joined
Oct 24, 2007
Messages
54
Hello you wise VBA sages! I have a little piece of VBA that runs on select files to lock it down in order to keep users from entering data or altering formatting. It works great! (Thank you!)

I've noticed, though, that is also locks any other files you may already have open. It has a password, so this obviously presents a problem, as the other file remains locked and it shouldn't be. How can I alter the code to only run on the file in which it is embedded? Or is there another solution I'm overlooking?

Here is the code:

Private Sub Workbook_Open()
'
' Protection Macro
' Macro written by Sharon Van Dyke
'
Dim Sheetnumber
Sheetnumber = 1
Do
ActiveWorkbook.Worksheets(Sheetnumber).Activate
ActiveWorkbook.Worksheets(Sheetnumber).Protect "surt"
Sheetnumber = Sheetnumber + 1
Loop Until Sheetnumber = ActiveWorkbook.Worksheets.Count
End Sub

Thank you in advance!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Maybe change ActiveWorkbook to ThisWorkbook or specify the workbook by name Workbooks("MineToEdit.xlsm").Worksheets(Sheetnumber).Protect "surt"
 
Upvote 0
Another way:

Code:
Private Sub Workbook_Open()
    Dim wks As Worksheet
    
    For Each wks In Me.Worksheets
        wks.Protect "surt"
    Next wks
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,051
Members
452,542
Latest member
Bricklin

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