How to close/exit VBA Form macro if conditions are success

gl3nnyb0y24

New Member
Joined
May 8, 2019
Messages
6
How to close/exit VBA Form if conditions are success;
(unprotect all sheets with a password)

- I created a form for Inputting passwords to unprotect all sheets
- Then click the Submit button to execute the code

If the password is correct the form did close, how do I close the form if the password is correct?

Thank you!

VBA Code:
Private Sub cmdInput_Click()

Dim ws As Worksheet
Dim pwd As String

pwd = txtPassword.Value

On Error Resume Next
For Each ws In Worksheets
ws.unprotect Password:=pwd
Next ws
If Err <> 0 Then
MsgBox "You have entered an incorrect password. Please check your password."
On Error GoTo 0

End If

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
If you're just trying to get the form to unload at the end of your code, this will do the trick:
VBA Code:
Private Sub cmdInput_Click()

Dim ws As Worksheet
Dim pwd As String

pwd = txtPassword.Value

On Error Resume Next
For Each ws In Worksheets
ws.Unprotect Password:=pwd
Next ws
If Err <> 0 Then
MsgBox "You have entered an incorrect password. Please check your password."
On Error GoTo 0
Exit Sub

End If

Unload Me

End Sub
 
Upvote 1
Try this:

Rich (BB code):
Private Sub cmdInput_Click()
  Dim ws As Worksheet
  Dim pwd As String
  
  pwd = txtPassword.Value
  
  On Error Resume Next
  For Each ws In Worksheets
    ws.Unprotect Password:=pwd
  Next ws
  
  If Err <> 0 Then
    MsgBox "You have entered an incorrect password. Please check your password."
    On Error GoTo 0
    txtPassword.SetFocus
    txtPassword.Value = ""    'If you want to clear textbox
  Else
    Unload Me
  End If
End Sub
 
Upvote 1
If you're just trying to get the form to unload at the end of your code, this will do the trick:
VBA Code:
Private Sub cmdInput_Click()

Dim ws As Worksheet
Dim pwd As String

pwd = txtPassword.Value

On Error Resume Next
For Each ws In Worksheets
ws.Unprotect Password:=pwd
Next ws
If Err <> 0 Then
MsgBox "You have entered an incorrect password. Please check your password."
On Error GoTo 0
Exit Sub

End If

Unload Me

End Sub
Hi Max1616, thank you very much for your help.
 
Upvote 0
Try this:

Rich (BB code):
Private Sub cmdInput_Click()
  Dim ws As Worksheet
  Dim pwd As String
 
  pwd = txtPassword.Value
 
  On Error Resume Next
  For Each ws In Worksheets
    ws.Unprotect Password:=pwd
  Next ws
 
  If Err <> 0 Then
    MsgBox "You have entered an incorrect password. Please check your password."
    On Error GoTo 0
    txtPassword.SetFocus
    txtPassword.Value = ""    'If you want to clear textbox
  Else
    Unload Me
  End If
End Sub
Hi DanteAmor, thank you very much for your help

btw, how do I add MsgBox "All sheets are now unprotected" after inputting the correct password?
 
Upvote 0
MsgBox "All sheets are now unprotected" after inputting the correct password?

Rich (BB code):
Private Sub cmdInput_Click()
  Dim ws As Worksheet
  Dim pwd As String
  
  pwd = txtPassword.Value
  
  On Error Resume Next
  For Each ws In Worksheets
    ws.Unprotect Password:=pwd
  Next ws
  
  If Err <> 0 Then
    MsgBox "You have entered an incorrect password. Please check your password."
    On Error GoTo 0
    txtPassword.SetFocus
    txtPassword.Value = ""    'If you want to clear textbox
  Else
    MsgBox "All sheets are now unprotected"
    Unload Me
  End If
End Sub
 
Upvote 1
Solution
Rich (BB code):
Private Sub cmdInput_Click()
  Dim ws As Worksheet
  Dim pwd As String
 
  pwd = txtPassword.Value
 
  On Error Resume Next
  For Each ws In Worksheets
    ws.Unprotect Password:=pwd
  Next ws
 
  If Err <> 0 Then
    MsgBox "You have entered an incorrect password. Please check your password."
    On Error GoTo 0
    txtPassword.SetFocus
    txtPassword.Value = ""    'If you want to clear textbox
  Else
    MsgBox "All sheets are now unprotected"
    Unload Me
  End If
End Sub
Hi DanteAmor, Again, thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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