PopUp InPutBox Again

Prasad K

Board Regular
Joined
Aug 4, 2021
Messages
189
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
Here i used this vba to unprotect sheet by inputbox

here what i want if type wrong password then a msgbox will popup and show me message (wrong password) then i click ok
the inputbox will be get popup again to enter correct password after i enter correct password then show me another msgbox (sheet unprotect successfully)



VBA Code:
Sub UnProtect()

Dim ws As Worksheet
Dim pwd As String
Dim myValue As Variant
pwd = "123" ' Put your password here
myValue = InputBox("What is the password?")
If myValue = pwd Then
    For Each ws In Worksheets
        ws.UnProtect Password:=pwd
    Next ws
Else
   MsgBox "Wrong Password"
End If

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi
try

VBA Code:
Sub Unprotect()
    Dim Entry       As Variant
    Dim strPrompt   As String, strTitle As String
    Dim try         As Long
    Dim ws          As Worksheet
  
    Const strPassword As String = "123"
  
    try = 1
    strTitle = "Enter Password"
    Do
        strPrompt = "Please Enter Password"
        strPrompt = strPrompt & Chr(10) & Chr(10) & _
                 "attempt " & try & " of 3"
      
        Entry = InputBox(strPrompt, strTitle)
        'cancel pressed
        If StrPtr(Entry) = 0 Then Exit Do
      
        If Entry <> strPassword And try = 3 Then
            MsgBox "Three Attempts Only Allowed", 16, "Three Attempts Only"
            Exit Do
        ElseIf Entry <> strPassword Then
            MsgBox "Password invalid - Please try again", 48, "Invalid Password"
            try = try + 1
          
        Else
          
            MsgBox "Password Correct - Click OK To Continue.", 48, "Password Correct"
          
            Exit Do
        End If
      
    Loop
    For Each ws In Worksheets
        ws.Unprotect Password:=Entry
    Next ws
End Sub

Dave
 
Upvote 0
Hi
try

VBA Code:
Sub Unprotect()
    Dim Entry       As Variant
    Dim strPrompt   As String, strTitle As String
    Dim try         As Long
    Dim ws          As Worksheet
 
    Const strPassword As String = "123"
 
    try = 1
    strTitle = "Enter Password"
    Do
        strPrompt = "Please Enter Password"
        strPrompt = strPrompt & Chr(10) & Chr(10) & _
                 "attempt " & try & " of 3"
     
        Entry = InputBox(strPrompt, strTitle)
        'cancel pressed
        If StrPtr(Entry) = 0 Then Exit Do
     
        If Entry <> strPassword And try = 3 Then
            MsgBox "Three Attempts Only Allowed", 16, "Three Attempts Only"
            Exit Do
        ElseIf Entry <> strPassword Then
            MsgBox "Password invalid - Please try again", 48, "Invalid Password"
            try = try + 1
         
        Else
         
            MsgBox "Password Correct - Click OK To Continue.", 48, "Password Correct"
         
            Exit Do
        End If
     
    Loop
    For Each ws In Worksheets
        ws.Unprotect Password:=Entry
    Next ws
End Sub

Dave
Thank you so much it's working

in 3rd attempt of wrong password i have Run-time Error on this line ws.Unprotect Password:=Entry
 
Upvote 0
Hi,

sorry my error

VBA Code:
Sub Unprotect()
    Dim Entry       As Variant
    Dim strPrompt   As String, strTitle As String
    Dim try         As Long
    Dim ws          As Worksheet
    
    Const strPassword As String = "123"
    
    try = 1
    strTitle = "Enter Password"
    Do
        strPrompt = "Please Enter Password To Continue"
        strPrompt = strPrompt & Chr(10) & Chr(10) & _
                 "attempt " & try & " of 3"
        
        Entry = InputBox(strPrompt, strTitle)
        If StrPtr(Entry) = 0 Then Exit Sub
        
        If Entry <> strPassword And try = 3 Then
            MsgBox "Three Attempts Only Allowed", 16, "Three Attempts Only"
            Exit Sub
        ElseIf Entry <> strPassword Then
            MsgBox "Password invalid - Please try again", 48, "Invalid Password"
            try = try + 1
            
        Else
            
            MsgBox "Password Correct - Click OK To Continue.", 48, "Password Correct"
            
            Exit Do
        End If
        
    Loop
    For Each ws In Worksheets
        ws.Unprotect Password:=Entry
    Next ws
End Sub



Dave
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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