VBA Protection Question

dfenton21

Board Regular
Joined
Jun 23, 2007
Messages
135
Hi,

I am using this code to protect all non-blank cells.

Private Sub Worksheet_Change(ByVal Target As Range)

ActiveSheet.EnableSelection = xlUnlockedCells

If Target <> "" Then
Unprotect
Target.Locked = True
Protect
End If
End Sub

I set up the protection so that the user can't select the protected cells. However, if I save and close the file, and reopen it, the cells are still protected, but the user can select them (but not change). Can I stop this from happening?

Thanks.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi,

Unfortunately, if you apply this setting via VBA code then the setting is not retained when the workbook is closed and re-opened.
It is, of course, retained if it is set manually via the Excel interface.

In terms of VBA, one option would be to enforce it in the workbook's open event handler. In the ThisWorkbook class module:
Rich (BB code):
Private Sub Workbook_Open()
    Sheet1.EnableSelection = xlUnlockedCells
End Sub
And change Sheet1 to the codename of the sheet in question.

Hope that helps...
 
Upvote 0

Forum statistics

Threads
1,215,494
Messages
6,125,137
Members
449,207
Latest member
VictorSiwiide

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