+ Reply to Thread
Results 1 to 22 of 22

Hide command button if worksheet is protected

Hybrid View

PRodgers4284 Hide command button if... 10-03-2009, 05:26 AM
DonkeyOte Re: Hide command button if... 10-03-2009, 05:29 AM
PRodgers4284 Re: Hide command button if... 10-03-2009, 05:34 AM
DonkeyOte Re: Hide command button if... 10-03-2009, 05:38 AM
PRodgers4284 Re: Hide command button if... 10-03-2009, 05:40 AM
DonkeyOte Re: Hide command button if... 10-03-2009, 05:42 AM
royUK Re: Hide command button if... 10-03-2009, 05:43 AM
PRodgers4284 Re: Hide command button if... 10-03-2009, 06:03 AM
DonkeyOte Re: Hide command button if... 10-03-2009, 06:09 AM
PRodgers4284 Re: Hide command button if... 10-03-2009, 06:30 AM
PRodgers4284 Re: Hide command button if... 10-05-2009, 04:41 AM
DonkeyOte Re: Hide command button if... 10-05-2009, 04:44 AM
PRodgers4284 Re: Hide command button if... 10-05-2009, 05:03 AM
DonkeyOte Re: Hide command button if... 10-05-2009, 05:31 AM
PRodgers4284 Re: Hide command button if... 10-05-2009, 05:57 AM
PRodgers4284 Re: Hide command button if... 10-05-2009, 07:05 AM
DonkeyOte Re: Hide command button if... 10-05-2009, 07:32 AM
  1. #1
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Hide command button if worksheet is protected

    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?

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    Questions

    1 - Command Button is Active X or Forms based control ?

    2 - How is the sheet protection being toggled ?

  3. #3
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    Quote Originally Posted by DonkeyOte View Post
    Questions

    1 - Command Button is Active X or Forms based control ?

    2 - How is the sheet protection being toggled ?

    DonkeyOte my command button is a form based control and the sheet is being protected using just the protect sheet facility within excel.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    ...the sheet is being protected using just the protect sheet facility within excel.
    this is a hard "event" to trap - ie the event (change to sheet protection) does not exist by default.

  5. #5
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    Quote Originally Posted by DonkeyOte View Post
    this is a hard "event" to trap - ie does not exist by default.
    DonkeyOte is there any other way i can protect the sheet?

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    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.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Hide command button if worksheet is protected

    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

  8. #8
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    Quote Originally Posted by royUK View Post
    Maybe if yu explai why you need to do this we might be able to sugget a better way.


    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.

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    i want this only to be available to me
    You could then perhaps use the Workbook_Open event to toggle visibility based on NT username...

    Private Sub Workbook_Open()
    Sheets("Sheet1").CommandButton1.Visible = UCase(Environ("username")) = "YOURNTUSERNAME"
    End Sub
    the above utilises an ActiveX Command Button but get the gist I hope ?

  10. #10
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    Quote Originally Posted by DonkeyOte View Post
    You could then perhaps use the Workbook_Open event to toggle visibility based on NT username...

    Private Sub Workbook_Open()
    Sheets("Sheet1").CommandButton1.Visible = UCase(Environ("username")) = "YOURNTUSERNAME"
    End Sub
    the above utilises an ActiveX Command Button but get the gist I hope ?

    DonkeyOte, i can use the network username used when i log into my own work network account is that what you mean?

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    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)

  12. #12
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    Quote Originally Posted by DonkeyOte View Post
    You could then perhaps use the Workbook_Open event to toggle visibility based on NT username...

    Private Sub Workbook_Open()
    Sheets("Sheet1").CommandButton1.Visible = UCase(Environ("username")) = "YOURNTUSERNAME"
    End Sub
    the above utilises an ActiveX Command Button but get the gist I hope ?

    DonkeyOte i cant get this to work, i have a button called "ButtonSave" that want to hide, but when i open the workbook in a different user account the button still appears.

  13. #13
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    Please post a sample file that reflects your exact setup (sheet name, button etc...) with code in place.

  14. #14
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    Sample workbook:
    Last edited by PRodgers4284; 10-05-2009 at 01:39 PM.

  15. #15
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    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

  16. #16
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    DonkeyOte thankyou very much, your help is very much appreciated, this works exactly the way i want

  17. #17
    Registered User
    Join Date
    04-22-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    94

    Re: Hide command button if worksheet is protected

    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.

  18. #18
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Hide command button if worksheet is protected

    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

    Public Sub Demo()
    Dim str1 As String, str2 As String
    str1 = "Donkey"
    str2 = "donKey"
    MsgBox str1 = str2
    End Sub
    generates FALSE (whereas in native XL: ="Donkey"="donKey" would generate TRUE)

    on that basis I tend to convert both strings to a common case before comparing so as to remove issue of case sensitivity

    Public Sub DemoTwo()
    Dim str1 As String, str2 As String
    str1 = "Donkey"
    str2 = "donKey"
    MsgBox UCase(str1) = UCase(str2)
    End Sub
    Generates TRUE as DONKEY = DONKEY

    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:

    MsgBox UCase(Environ("username")) = "DONKEY"
    Given my NT login will be forced to Upper Case in the first part of the test.

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1