调用用户窗体并返回值 [英] Calling a userform and returning a value

查看:178
本文介绍了调用用户窗体并返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vba代码,Auto_Open。它会进行一些检查,然后提示用户表单要求用户名和密码。我使用 userform_name.show 调用此用户窗体。



我的问题是如何返回一个

我把代码链接到我的 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


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 userform_name.show.

My issue is how can I return a Boolean to my Auto_Open sub from the userform code.

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.

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

解决方案

Remove Dim bool As Boolean from the userform code area and declare it in the module as shown below

This is how your Code in the module would look like

Public 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

这篇关于调用用户窗体并返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆