Avatar of Bright01
Bright01
Flag for United States of America asked on

Macro to Lock and Unlock a Worksheet ONLY

EE Pros,

What would the code look like to select a Lock and Unlock capability in a single Worksheet?  Here's the tricky part.  How do you switch icons to represent the condition or status (Locked or Unlocked) on the sheet?
The icon should change when selected.

Thank you in advance!

B.
Lock.jpg
Unlock.jpg
Microsoft ExcelMicrosoft Office

Avatar of undefined
Last Comment
Bright01

8/22/2022 - Mon
Mike in IT

You would use something like what is found on this page. It could look something like this:
 .Protect(Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)

Open in new window


Obviously, you don't necessarily need all of those options. You can read about them on the link above.
Bright01

ASKER
Mike in IT,  Thanks for the thought, however, unfortunately, I don't know how to write the several lines of code that use the commands you referred to.

B.
Martin Liss

Worksheets("MySheet").Protect UserInterfaceOnly:=True, DrawingObjects:=True, Contents:=True...

or

ActiveSheet.Protect UserInterfaceOnly:=True, DrawingObjects:=True, Contents:=True...

and then

ActiveSheet.Unprotect

or

Worksheets("MySheet").UnProtect
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bright01

ASKER
Martin,

Greetings.  Thanks for jumping in.  With the code you provided, I sketched up a mock WS.  I can't get it to work but thought this might show exactly what I'm looking for.

Again, thanks to both of you,

B.
Lock-Unlock-Macro.xlsm
Mike in IT

I have updated the code that you had on your sheet and assigned the pictures to run one of the subs. Now when you click on the locked picture it locks the sheet and when you click on the unlock icon it unlocks the sheet.
Lock-Unlock-Macro.xlsm
Bright01

ASKER
Mike and Martin,

GREAT........ALMOST THERE!!!!   Re-read the original "ask".  

Overlay the two icons.  When you click on one of the icons, the other appears.... and vice versa.  Only the next option (Lock or Unlock) is shown. This gives the impression of a single button that locks and unlocks the worksheet.  

Thank you,

B.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

I'll let Mike answer, but you probably don't need two icons. Just use one and programmatically change what it says and does after it's clicked.
Bright01

ASKER
How hard is it to use the icons?

B.
ASKER CERTIFIED SOLUTION
Mike in IT

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Bright01

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Martin Liss

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bright01

ASKER
Great work guys in helping me through this apparently simple macro.  I'm going to author one more on this.  Hope one of you will pick it up.

All the best,

B.
Your help has saved me hundreds of hours of internet surfing.
fblack61