flowers1234
flowers1234

Reputation: 347

Excel - VBA Check if worksheet is unprotected

With if Worksheets("test").ProtectContents I can recognize if the worksheet is protected. It only returns a true, right?

How can I check if it is unprotected?

If Worksheets("test").ProtectContents = True Then: Exit Sub:

For Each cell In Range("B6:B112")
...
Next cell

Else:

If Not Worksheets("test").ProtectContents Then
Dim rng As Range
...

The foreach loop have to execute when the sheet is protected. And the part at if not worksheet.. is for the part when the sheet is unproteced. thx.

Upvotes: 1

Views: 6821

Answers (2)

Vityata
Vityata

Reputation: 43585

Try like this:

If Not Worksheets("test").ProtectContents Then Exit Sub

Upvotes: 0

Kalpesh Koli
Kalpesh Koli

Reputation: 87

I guess this is what you are looking for,

If ActiveSheet.ProtectContents = True Then
MsgBox "Protected"
Else
MsgBox "Not protected"
End If

if not, please comment. hopefully we shall able to resolve your issue.

Upvotes: 4

Related Questions