My macro will not run b/c the sheet is protected

sneel3

Active Member
Joined
Oct 9, 2002
Messages
334
how can I have the macro turn off the protection at the beginning and turn it back on at the end?
 
The password is "FALSE" (alternatively, it is "TRUE"). In
Code:
Option Explicit

Sub testProtect()
    Dim Password As String
    ActiveSheet.Protect Password = "stu"
    End Sub
or
Code:
Sub testProtect()
    ActiveSheet.Protect Password = "stu"
    End Sub
VBA evaluates 'password="stu"' as a logical test that returns either TRUE or FALSE. Assuming the variable named password doesn't contain the string "stu", the code becomes ActiveSheet.Protect FALSE and VBA coerces the boolean into a string to get ActiveSheet.Protect "FALSE"
joe have you tested what im saying????

cause as a quote from you

ActiveSheet.Protect password = "stu"
is the same as:

ActiveSheet.Protect

it isnt the same at all
cause
ActiveSheet.Protect
wouldnt want a password to unprotect

but


ActiveSheet.Protect password = "stu"

does expect a password to unprotect

ande the password isnt "stu"

hey hey

Stu
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Tushar,

This is new to me!
I tested it and this wrong syntax, does work with a passwoed of:

FALSE

and it will not take any other format of this, like:
False, false, -1, 0, 1

This is a good thing to know!

It never occured to me to try a Boolean. I like it, when I learn something new, thanks.
 
Upvote 0
Yeah, that's the problem with languages like VB (and others like C) which coerce data types willy-nilly.

Of course the problem is mitigated -- not eliminated, mind you -- if Option Explicit were set.

I am not sure I understand the problem with the other values.
Code:
ActiveSheet.Protect False
sets the password to FALSE (all upcase)
Code:
ActiveSheet.Protect -1
sets the PW to -1

Essentially, the key here is that the syntax is
.Protect {string}
and anything you provide that can even remotely be converted into a string will be -- at times with unexpected and maybe even potentially disastrous results.

Tushar,

This is new to me!
I tested it and this wrong syntax, does work with a passwoed of:

FALSE

and it will not take any other format of this, like:
False, false, -1, 0, 1

This is a good thing to know!

It never occured to me to try a Boolean. I like it, when I learn something new, thanks.
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,392
Members
449,445
Latest member
JJFabEngineering

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