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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,563
Messages
6,114,332
Members
448,566
Latest member
Nickdozaj

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
Back
Top