调用用户窗体并返回值 [英] Calling a userform and returning a value
问题描述
userform_name.show
调用此用户窗体。 我的问题是如何返回一个 我把代码链接到我的 删除 这是你的模块中的代码如何看起来像 I have a vba code thats Auto_Open. It does some checks then prompts a userform that asks for username and password. I called this userform with My issue is how can I return a I linked the code that verifies if the credentials are correct to the "Login" button on the form. this is the code that produces the Boolean. I need to return it to the Auto_Open.
Remove This is how your Code in the module would look like
这篇关于调用用户窗体并返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Auto_Open
它验证表单上的登录按钮的凭据是否正确。这是生成布尔值的代码。我需要将其返回到Auto_Open。
Private Sub loginbutton()
Dim bool As Boolean
Dim lrup
Dim r As Long
Dim pass As String
loginbox.Hide
'是字段空的
Do While True
如果unBox.Text =或pwBox.Text =然后
MsgBox(您必须输入用户名和密码)
Else
退出Do
结束If
loginbox.Show
退出子
循环
'查找pw reated到用户名(如果存在)
lrup = UserPass.Range(A1) .Offset(UserPass.Rows.Count - 1,0).End(xlUp).Row
如果unBox =b0541476和pwBox =theone然后
bool = True
Else
MsgBox(无效的用户名或密码,请重试。)
loginbox.Show
退出子
结束如果
对于r = 2 To lrup
如果unBox = Cells(r,1)Then
pass = Cells(r,2).Value
退出
结束如果
下一个
如果pass =然后
MsgBox(无效的用户名或密码。请重试。)
loginbox.Show
退出子
Else
bool = True
如果
结束Sub
Dim bool As Boolean
从用户形式的代码区域,并在模块中声明它如下所示
公开bool As Boolean
Sub Auto_Open()
'
'~~>其余的代码
'
UserForm1.Show
如果bool = True然后
'~~>做某事
Else
'~~>做某事
结束如果
'
'~~>其余代码
'
End Sub
userform_name.show
.Boolean
to my Auto_Open
sub from the userform code.Private Sub loginbutton()
Dim bool As Boolean
Dim lrup
Dim r As Long
Dim pass As String
loginbox.Hide
'are fields empty
Do While True
If unBox.Text = "" Or pwBox.Text = "" Then
MsgBox ("You must enter a Username and Password")
Else
Exit Do
End If
loginbox.Show
Exit Sub
Loop
'find pw reated to username (if existant)
lrup = UserPass.Range("A1").Offset(UserPass.Rows.Count - 1, 0).End(xlUp).Row
If unBox = "b0541476" And pwBox = "theone" Then
bool = True
Else
MsgBox ("Invalid username or password. Please try again.")
loginbox.Show
Exit Sub
End If
For r = 2 To lrup
If unBox = Cells(r, 1) Then
pass = Cells(r, 2).Value
Exit For
End If
Next
If pass = "" Then
MsgBox ("Invalid username or password. Please try again.")
loginbox.Show
Exit Sub
Else
bool = True
End If
End Sub
Dim bool As Boolean
from the userform code area and declare it in the module as shown belowPublic bool As Boolean
Sub Auto_Open()
'
'~~> Rest of the code
'
UserForm1.Show
If bool = True Then
'~~> Do Something
Else
'~~> Do Something
End If
'
'~~> Rest of the code
'
End Sub