Prompt for Password

kev6264

New Member
Joined
Jun 16, 2006
Messages
44
How do I include a prompt for a user to enter a password when unprotecting a worksheet. Here is the procedure.. I have to unprotect sheet to sort and then I want to re-protect afterwards.

Code:
Range("C5:L1500").Select
    
    Selection.Sort Key1:=Range("C6"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Range("C6").Select
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Hi there,

Maybe something like this ..

Code:
Sub ShowPandUP()
    Dim strPwd As Variant
    strPwd = InputBox("Please enter password:", "PASSWORD", "Type password...")
    If strPwd <> "password" Then
        MsgBox "Nope, wrong password.", vbInformation, "NO WAY HOSE"
        Exit Sub
    End If
    With Sheets("Sheet1")
        .Unprotect Password:=strPwd
        .Range("C5:L1500").Sort Key1:=.Range("C6"), Order1:=xlAscending, Header:=xlGuess, _
                   OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                   DataOption1:=xlSortNormal
        .Protect Password:=strPwd
    End With
End Sub
 

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
Something like this
Code:
Sub Password()
    Dim pwd As String
    pwd = InputBox("Enter password")
    If pwd <> "password" Then
    MsgBox "Wrong Password"
    Exit Sub
    End If
    ActiveSheet.Unprotect pwd
End Sub

lenze
 

Smitty

Legend
Joined
May 15, 2003
Messages
29,536
Maybe something like this:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> MySort()
    <SPAN style="color:#00007F">Dim</SPAN> pword <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    <SPAN style="color:#00007F">Dim</SPAN> x
        pword = InputBox("Please enter a password", "Password")
        
        <SPAN style="color:#00007F">If</SPAN> pword <> "foo" <SPAN style="color:#00007F">Then</SPAN>
            x = MsgBox("Incorrect Password entered!  Retry?", _
                vbCritical + vbYesNoCancel, "Incorrect Pasword")
            <SPAN style="color:#00007F">If</SPAN> x = vbYes <SPAN style="color:#00007F">Then</SPAN>
                MySort
            Else:   <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
            
        ActiveSheet.Unprotect pword
            Range("C5:L1500").Sort Key1:=Range("C6"), Order1:=xlAscending, Header:=xlGuess, _
                OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                DataOption1:=xlSortNormal
        ActiveSheet.Protect pword
        
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,

Smitty
 

Forum statistics

Threads
1,136,706
Messages
5,677,309
Members
419,685
Latest member
hennLow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top