在电子表格中,我有一个链接到以下VBA的按钮:
Sub Button_01()
If UserForm1.checkPassword() = True Then
Sheet1.Range("C3").Value = 1
Else
End If
End Sub
使用此按钮,我触发一个具有以下VBA的用户表单:
Private passwordStatus As Boolean
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
passwordStatus = False
If Password = a Then
MsgBox "Password Correct.", vbInformation
passwordStatus = True
Unload Me
Else
MsgBox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
Function checkPassword() As Boolean
UserForm1.Show
checkPassword = passwordStatus
End Function
UserForm1
可以打开,没有任何问题,但是当我在用户窗体中输入密码时,该过程不会继续。 The UserForm1
呆在那里。
输入密码后,我无法在代码中找到阻止宏继续的错误。你能帮我吗?
答案 0 :(得分:1)
对我来说,除了:
Sub Button_01()
If UserForm1.checkPassword() = True Then
Sheet1.Range("C3").Value = 1
Else
End If
End Sub
这段代码将出错。
更改:
Sheet1.Range("C3").Value = 1
收件人:
Sheets(1).Range("C3").Value = 1
答案 1 :(得分:1)
通常通过公共变量在用户窗体和常规模块之间传递信息。您应该将大部分代码放入常规模块中,并且仅使用用户表单将检查返回。
常规模块:
Public CorrectPassword As Boolean
Sub Button_01()
CorrectPassword = False
UserForm1.Show
If CorrectPassword = True Then
Sheets(1).Range("C3").Value = 1
Else
Msgbox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
用户表单模块:
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
If Password = a Then CorrectPassword = True
Unload Me
End Sub