Modifying existing code to double prompt for password..

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey Excel Gurus,

How are you? I have a code that protects all sheets in a workbook.
VBA Code:
Sub ProtectAll()
Dim wSheet As Worksheet
Dim Pwd As String
Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
For Each wSheet In Worksheets
wSheet.Protect Password:=Pwd
Next wSheet
End Sub

I want to add two more features to it.
1. Ask the user to enter the same password twice.
2. I need a check mark option to hide the password with round circles for each character input. Thus by default the user can view the password but check marking "Hide Password" should hide the password.

Will appreciate plenty,

Thanks.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
1. Ask the user to enter the same password twice.
2. I need a check mark option to hide the password with round circles for each character input. Thus by default the user can view the password but check marking "Hide Password" should hide the password.

Hi,
first part of your requirement is quite straight forward

try this modified code & see if does what you want

VBA Code:
Sub ProtectAll()
    Dim wSheet As Worksheet
    Dim Pwd(1 To 2) As String
    Dim i As Integer
    
    Do
        i = i + 1
        Pwd(i) = InputBox("Enter your password " & _
                          Choose(i, "to protect all worksheets", "again"), "Password Input -" & i)
'cancel pressed
        If StrPtr(Pwd(i)) = 0 Then Exit Sub
        If i = 2 And Pwd(1) <> Pwd(2) Then
            MsgBox "Passwords Do Not Match" & Chr(10) & "Please Try Again", 48, "Passwords Do Not Match"
            i = 0
        End If
    Loop Until Pwd(1) = Pwd(2)
    
    For Each wSheet In Worksheets
        wSheet.Protect Password:=Pwd(1)
    Next wSheet
End Sub

2nd part is a little more complex - whilst masking Inputbox entry has been done, it requires some quite extensive coding but you can achieve same result much easier with a userform.

Have a look here for an example:Excel Userform Login - Online PC Learning

Hope Helpful

Dave
 
Upvote 0
Hi,
first part of your requirement is quite straight forward

try this modified code & see if does what you want

VBA Code:
Sub ProtectAll()
    Dim wSheet As Worksheet
    Dim Pwd(1 To 2) As String
    Dim i As Integer
   
    Do
        i = i + 1
        Pwd(i) = InputBox("Enter your password " & _
                          Choose(i, "to protect all worksheets", "again"), "Password Input -" & i)
'cancel pressed
        If StrPtr(Pwd(i)) = 0 Then Exit Sub
        If i = 2 And Pwd(1) <> Pwd(2) Then
            MsgBox "Passwords Do Not Match" & Chr(10) & "Please Try Again", 48, "Passwords Do Not Match"
            i = 0
        End If
    Loop Until Pwd(1) = Pwd(2)
   
    For Each wSheet In Worksheets
        wSheet.Protect Password:=Pwd(1)
    Next wSheet
End Sub

2nd part is a little more complex - whilst masking Inputbox entry has been done, it requires some quite extensive coding but you can achieve same result much easier with a userform.

Have a look here for an example:Excel Userform Login - Online PC Learning

Hope Helpful

Dave
This works perfect. Thanks. I would rather stick with your code than trying something new with the UserForm. One question though I have is how about if I mask the input characters by default. Because I will be prompted twice, hence looking the password isn't that necessary afterall. What will change in the code to do this please?
 
Upvote 0
This works perfect. Thanks. I would rather stick with your code than trying something new with the UserForm. One question though I have is how about if I mask the input characters by default. Because I will be prompted twice, hence looking the password isn't that necessary afterall. What will change in the code to do this please?

Not sure how you intend to "mask" the input characters without resorting to some complex coding which can be better seen & explained here:

Personally, I think It would be far simpler to write some VBA to emulate the standard Inputbox with a userform where you can include your requirement for a checkbox to display the password.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

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