Cell/worksheet protection with macros enabled

Z

Zilla

I'm using a VBA macro courtesy of Debra D. on this NG
to temproraily resize the column for a drop-down list. All
work fine.

Now I've lock some of these cells that this macro runs
on, and enabled sheet protection on "Contents" only.
Now when I use my spreadsheet, I get an Run-time
Error 1004 - Unable to set column width property of
the range class. I've tried protection with either "Objects"
or "Scenarios" only checked, to no avail.

I gues this means I can not run macros that muck with
cell "properties" with sheet protection?
 
J

JLatham

You can unprotect the sheet incode, do your processing then put it back into
protected mode. Code something like this:

Sub PasswordsInCode()
ActiveSheet.Unprotect Password:="sheetpasswordhere"
' your processing here
ActiveSheet.Protect Password:="sheetpasswordhere", _
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True
End Sub
 
Z

Zilla

Thanks. if the sheet is NOT password protected, will these be the
3 lines I would need to put in-line with existing code?
DrawingObjects:=True, _
Contents:=True, _
Scenarios:=True

Is that last line also "=True._" or really "=True" as you've typed it.

-Zilla
 
Z

Zilla

Also, how can I just process the "unlocked" cells? IOW,
if cell is locked, don't run your code!

-Zilla
 
Z

Zilla

I found how to check for locked cells, but how do I
negate the check? Like

If !Target.Cells.Locked Then
 
J

JLatham

Those three lines are continuations of the .Protect statement, so don't leave
them in there all by their lonesome. You can determine exactly what
parameters you HAVE to have when protecting the sheet again by unprotecting
the sheet, then recording a macro to protect it with the options you want
set/not set and then look at the macro created. It will not record the
password you provide, you just stick that in at the beginning of the created
code as I did in the example.

Yes,
if Target.Cells.Locked = False Then
which is a straight-forward (and more easily understood) way of saying
If Not (Target.Cells.Locked = True) Then
but both achieve the same result.

You can 'shorthand' it this way also:
If Not(Target.Cells.Locked) Then
....
 
Z

Zilla

Thanks!

JLatham said:
Those three lines are continuations of the .Protect statement, so don't leave
them in there all by their lonesome. You can determine exactly what
parameters you HAVE to have when protecting the sheet again by unprotecting
the sheet, then recording a macro to protect it with the options you want
set/not set and then look at the macro created. It will not record the
password you provide, you just stick that in at the beginning of the created
code as I did in the example.

Yes,
if Target.Cells.Locked = False Then
which is a straight-forward (and more easily understood) way of saying
If Not (Target.Cells.Locked = True) Then
but both achieve the same result.

You can 'shorthand' it this way also:
If Not(Target.Cells.Locked) Then
...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top