我已经在Excel VBA中编写了以下用户窗体:
Private Sub CommandButton1_Click()
Password = TextBox1.Text
If Password = a Then
MsgBox "Password Correct.", vbInformation
Unload Me
Else
MsgBox "Password Incorrect. Please try again.", vbCritical
End If
End Sub
密码变量a =“ test”
该密码用户表单本身可以完美运行。现在,我想将其连接到工作表中已经存在的按钮。该按钮后面的代码在VBA编辑器的“ ThisWorkbook”部分中:
Sub Button_Test()
UserForm1.Show
Tabelle2.Range("C3").Value = 1
End Sub
这个想法是,按钮“ Button_Test”受用户窗体可以输入的密码保护。正确输入密码后,将执行宏“ Tabelle2.Range(“ C3”)。Value = 1“。
但是,当前我遇到的问题是,用户窗体使用“密码正确”响应我,但没有启动宏“ Tabelle2.Range(“ C3”)。Value = 1“。
输入正确的密码后,我该如何告诉用户表单,应跳回到宏“ Button_Test”并继续执行该过程?
谢谢你的帮助。
我将使用一个布尔值,当密码正确时,您将其设置为true。您可以做这样的事情:
模块或工作表中的代码
Sub Schaltfläche1_Klicken()
'Load funktion Check Password
If UserForm1.checkPassword() = True Then
'Run this Code when PWD was Correct
Tabelle1.Range("C3").Value = 1
End If
End Sub
在用户窗体中,您可以输入如下代码:
Private passwordStatus As Boolean
Private Sub CommandButton1_Click()
Dim a As String
Dim Password As String
a = "123"
Password = TextBox1.Text
'Set Pawwordstatus at False before Testing
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
'Shows the User Form. And after Closing the Form
'The PasswordStatus Value will be returned and you can check if
'it is true
checkPassword = passwordStatus
End Function
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句