+ Reply to Thread
Results 1 to 7 of 7

Password protect macro execution

  1. #1
    Registered User
    Join Date
    02-16-2022
    Location
    England
    MS-Off Ver
    2112
    Posts
    4

    Password protect macro execution

    Hi

    I was wondering if someone would be able to help me, please?

    I've created two separate "buttons" for a spreadsheet that filters information viewable based on whether or not specific text has been used.

    The code I've used to do this is:

    Button one
    Sub HURows2()
    BeginRow = 13
    EndRow = 128
    ChkCol = 8

    For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = "Light" Then
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    Else
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    End If
    Next RowCnt

    End Sub

    Button two
    Sub HURows1()
    BeginRow = 13
    EndRow = 128
    ChkCol = 8

    For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = "Master" Then
    Cells(RowCnt, ChkCol).EntireRow.Hidden = True
    Else
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    End If
    Next RowCnt
    End Sub



    I now want to make it so those two buttons are password protected and only those with the password can execute the macro. I've tried a variety of different codes and I've found one which kind of does what I want it to do, but not completely. This code is:

    Private Sub CommandButton1_Click()

    Dim password As Variant
    password = Application.InputBox("Enter Password", "Protected Macro")

    Select Case password
    Case Is = False
    'do nothing

    Case Is = "SocialValue"

    Sub HURows2()
    BeginRow = 13
    EndRow = 128
    ChkCol = 8

    For RowCnt = BeginRow To EndRow
    If Cells(RowCnt, ChkCol).Value = "Light" Then
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    Else
    Cells(RowCnt, ChkCol).EntireRow.Hidden = False
    End If
    Next RowCnt


    Case Else
    MsgBox "Incorrect Password"

    End Select

    End Sub


    The problem that I'm having with this is two-fold; the first is that it's erroring "Case Else outside Select Case"; if I add "Select Case password" above "Case Else", a box just pops up to say "Incorrect Password" but doesn't provide me with the opportunity to input a password.


    Apologies for the really long post, but could someone please help me? I'm at my wit's end and not very experienced in doing VBA.

    Thank you.

  2. #2
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Password protect macro execution

    You cannot start another sub inside a sub. You would have to call the sub like this -

    Please Login or Register  to view this content.
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

  3. #3
    Registered User
    Join Date
    02-16-2022
    Location
    England
    MS-Off Ver
    2112
    Posts
    4

    Re: Password protect macro execution

    Thank you for responding; I really appreciate it.

    I've tried using what you've suggested but the input box doesn't appear for the password and seems to automatically execute the macro.

  4. #4
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Password protect macro execution

    It does work fine for me. Please check again at your end or attach the workbook here so we can have a look.

    PS: I think you might be directly running sub "HURows2" and not "CommandButton1_Click".
    Also, instructions to attach a file are on the top (yellow ribbon).

  5. #5
    Registered User
    Join Date
    02-16-2022
    Location
    England
    MS-Off Ver
    2112
    Posts
    4

    Re: Password protect macro execution

    Thank you for looking into this for me. I've (hopefully) attached the workbook.
    Attached Files Attached Files

  6. #6
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: Password protect macro execution

    In the code I shared, remove the "Private" keyword from the first statement. Then, right click the button you want to assign the macro to >> Assign Macro >> "CommandButton1_Click()". Then try clicking the button and let me know if that works.

  7. #7
    Registered User
    Join Date
    02-16-2022
    Location
    England
    MS-Off Ver
    2112
    Posts
    4

    Re: Password protect macro execution

    That's amazing; thank you so so much! Honestly, I really appreciate it! It works like a charm!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Password Protect Workaround: Using Macro to act like WS Protect
    By jaredmccullough in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-31-2015, 11:04 AM
  2. Replies: 1
    Last Post: 05-27-2014, 02:40 PM
  3. How to protect formula & macro protect without password ?
    By nur2544 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-26-2012, 05:24 AM
  4. Password Protect Macro
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-22-2008, 10:21 AM
  5. password protect macro
    By Modell in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-27-2006, 04:10 PM
  6. Password Protect Macro?
    By ryanmhess in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-27-2006, 01:10 PM
  7. Password required before Macro execution
    By Andy Tallent in forum Excel General
    Replies: 4
    Last Post: 02-04-2005, 12:06 PM

Tags for this Thread

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