ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Command Button (https://www.excelbanter.com/excel-programming/384386-command-button.html)

Jordan

Command Button
 
I would like to be able to protect a sheet if it currently is not protected
and unprotect the sheet if it is protected. I know how to record a macro and
I think I can figure out how to get it to work on a button, but I would need
two buttons.

I was wondering if it was possible to have one button that would check the
status and reverse it. Also, if it is possible how would I get the name on
the button to change?

Any help will be greatly appreciated. I don't know allot about programming
so talk real slow.

Jim Thomlinson

Command Button
 
In XL show the Control Toolbox (Tools - Customize - Toolbars - Check
Control Toolbox). Drag a Button off the toolbar and place it on the sheet.
Right click the sheet tab and select view code. Paste the following code...

Private Sub CommandButton1_Click()

With ActiveSheet
If .ProtectContents = True Then
.Unprotect
CommandButton1.Caption = "Unprotected"
Else
.Protect
CommandButton1.Caption = "Protected"
End If
End With
End Sub
--
HTH...

Jim Thomlinson


"Jordan" wrote:

I would like to be able to protect a sheet if it currently is not protected
and unprotect the sheet if it is protected. I know how to record a macro and
I think I can figure out how to get it to work on a button, but I would need
two buttons.

I was wondering if it was possible to have one button that would check the
status and reverse it. Also, if it is possible how would I get the name on
the button to change?

Any help will be greatly appreciated. I don't know allot about programming
so talk real slow.


Jordan

Command Button
 
Jim, Thank you so much. It works perfect and unless the folks at my work are
perusing the discussion group, they are going to think I really know what I'm
doing.

Thanks again.

"Jim Thomlinson" wrote:

In XL show the Control Toolbox (Tools - Customize - Toolbars - Check
Control Toolbox). Drag a Button off the toolbar and place it on the sheet.
Right click the sheet tab and select view code. Paste the following code...

Private Sub CommandButton1_Click()

With ActiveSheet
If .ProtectContents = True Then
.Unprotect
CommandButton1.Caption = "Unprotected"
Else
.Protect
CommandButton1.Caption = "Protected"
End If
End With
End Sub
--
HTH...

Jim Thomlinson


"Jordan" wrote:

I would like to be able to protect a sheet if it currently is not protected
and unprotect the sheet if it is protected. I know how to record a macro and
I think I can figure out how to get it to work on a button, but I would need
two buttons.

I was wondering if it was possible to have one button that would check the
status and reverse it. Also, if it is possible how would I get the name on
the button to change?

Any help will be greatly appreciated. I don't know allot about programming
so talk real slow.



All times are GMT +1. The time now is 01:47 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com