I want to hide a command button when a worksheet is protected and unhide when the sheet is unprotected, can anyone help me with this?
I want to hide a command button when a worksheet is protected and unhide when the sheet is unprotected, can anyone help me with this?
Questions
1 - Command Button is Active X or Forms based control ?
2 - How is the sheet protection being toggled ?
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
this is a hard "event" to trap - ie the event (change to sheet protection) does not exist by default....the sheet is being protected using just the protect sheet facility within excel.
You can use a Sub Routine (via Command Button etc) but again this does not prevent the user from protecting / unprotecting the sheet by "normal" means and toggling sheet protection via normal means is not a Volatile action.
Maybe if yu explai why you need to do this we might be able to sugget a better way.
Hope that helps.
RoyUK
--------
For Excel Tips & Solutions, free examples and tutorials why not check out my web site
Free DataBaseForm example
Roy i have designed a userform within a worksheet and a link to this will be added to an intranet site for users to fill out. The user saves a copy of the worksheet from the intranet and then i have a button that emails the worksheet once it is completed to my email address defined within the code, i then have a button within my worksheet form to save the worksheet to my desktop, i want this only to be available to me, this is the button i want hidden, so if i unprotect the sheet the button appears and i can save the worksheet. I hope this makes sense, if there is an easier way to do this i would really appreciate the help/advise.
You could then perhaps use the Workbook_Open event to toggle visibility based on NT username...i want this only to be available to me
the above utilises an ActiveX Command Button but get the gist I hope ?![]()
Private Sub Workbook_Open() Sheets("Sheet1").CommandButton1.Visible = UCase(Environ("username")) = "YOURNTUSERNAME" End Sub
Yes, correct.
It's a much safer way of ascertaining current user than Application.Username givne the latter can be easily modified via Excel Options... the former is pretty bullet proof unless someone has logged on to a machine using someone else's credentials at which point you have bigger fish to fry.
Remember of course
a) you will want to password protect your VBE Project
b) no security in XL is 100% watertight (those in the know will always find a way to circumvent XL protection)
Please post a sample file that reflects your exact setup (sheet name, button etc...) with code in place.
Sample workbook:
Last edited by PRodgers4284; 10-05-2009 at 01:39 PM.
OK a few points...
1 - workbook level events (like Workbook_Open) should reside in the Workbook Object which is called ThisWorkbook (ie not in Sheet Object nor standard Module)
2 - you refer to Sheets("Sheet3") ... the button is seemingly on sheet called "Template" which happens to have Sheet3 codename so to refer to this sheet use either
a) Sheet3
or
b) Sheets("Template")
3 - the code I gave originally was for an ActiveX Command Button rather than a Forms Command Button... you're using the latter so you will need to revise the code accordingly
4 - given neither Sheet1 nor Sheet2 (codenames) exist in your sample file you need to remove those lines
So putting all of the above together the below should be added to ThisWorkbook Object in your sample file
![]()
Private Sub Workbook_Open() With Sheet3 .ScrollArea = "A1:AB23" .Shapes("ButtonSave").Visible = UCase(Environ("username")) = "MYUSERNAME" End With End Sub
Last edited by DonkeyOte; 10-05-2009 at 05:35 AM. Reason: typos
DonkeyOte thankyou very much, your help is very much appreciated, this works exactly the way i want![]()
I couldnt same to get the username to work, i was aware the username had to be in caps to be recognized by the code.
Last edited by PRodgers4284; 10-05-2009 at 07:08 AM.
Again, not sure I follow but just to elaborate in terms of the use of UCase etc...
By default VBA is case-sensitive unlike native XL, eg
generates FALSE (whereas in native XL: ="Donkey"="donKey" would generate TRUE)![]()
Public Sub Demo() Dim str1 As String, str2 As String str1 = "Donkey" str2 = "donKey" MsgBox str1 = str2 End Sub
on that basis I tend to convert both strings to a common case before comparing so as to remove issue of case sensitivity
Generates TRUE as DONKEY = DONKEY![]()
Public Sub DemoTwo() Dim str1 As String, str2 As String str1 = "Donkey" str2 = "donKey" MsgBox UCase(str1) = UCase(str2) End Sub
In your case the str1 is the Environ("username") and str2 is your own NT login, so given I am going to coerce str1 to Upper Case I must ensure that the test value is also in Upper Case, ie even if my real NT ID is donKey I would use:
Given my NT login will be forced to Upper Case in the first part of the test.![]()
MsgBox UCase(Environ("username")) = "DONKEY"
You can also use Option Compare Text at the head of a Module to indicate all comparisons should be case insensitive, however, in some cases you may want case sensitivity so I prefer the above method myself as a general rule
Last edited by DonkeyOte; 10-05-2009 at 07:34 AM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks