为什么Userform在隐藏并多次显示后会冻结

时间:2013-11-21 01:09:09

标签: vba excel-vba excel-2010 excel

好的,我对隐藏和显示用户形式有疑问 This link已经为我回答了。

问题是我遇到了另一个问题 当我回到Userform1时,它会冻结,我根本无法做任何事情 为什么?我需要在代码中添加一些内容吗?

下面是我使用的代码的总结:
此代码提示用户输入用户名和密码

Option Explicit
Private Sub CBu_Login_Click()
Dim ws As Worksheet, rng As Range, lrow As Long, find_value As String
Dim cel As Range

Set ws = ThisWorkbook.Sheets("UserName")
lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("A2:A" & lrow)
find_value = Me.TB_Username.Value
Set cel = rng.Find(What:=find_value, After:=ws.Range("A2"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
If Not cel Is Nothing Then
    If Me.TB_Password.Value = cel.Offset(0, 1).Value Then
        Me.Hide
        UF_Encoding.L_User.Caption = "Welcome " & cel.Offset(0, 2).Value & "!" & " You are logged in."
        UF_Encoding.TB_Operator.Text = cel.Offset(0, 2).Value
        UF_Encoding.TB_ESN_IMEI.Value = ""
        UF_Encoding.CB_PrimaryCode.Value = ""
        UF_Encoding.CB_SecondaryCode.Value = ""
        UF_Encoding.TB_Remarks.Value = ""
        UF_Encoding.TB_ESN_IMEI.SetFocus
        UF_Encoding.Show
    Else
        MsgBox "Invalid Username/Password"
    End If
Else
    MsgBox "Invalid Username/Password"
End If

End Sub

此代码用于退出:
我在这里使用了Listbox,因此用户可以选择要采取的操作。

Private Sub LB_Options_AfterUpdate()

If Me.LB_Options.Value = "Log out" Then
    Me.Hide
    Me.LB_Options.Visible = False
    UF_Login.TB_Username.Value = ""
    UF_Login.TB_Password.Value = ""
    UF_Login.Show

ElseIf Me.LB_Options.Value = "Change Password" Then
    Me.Hide
    Me.LB_Options.Visible = False
    UF_Changepass.TB_User.Value = ""
    UF_Changepass.TB_Newpass.Value = ""
    UF_Changepass.TB_Oldpass.Value = ""
    UF_Changepass.Show

ElseIf Me.LB_Options.Value = "Exit" Then
    Me.Hide
    wbDbase.Save
    wbDbase.Close
    wbEncoding.Save
    wbEncoding.Close
    Unload UF_Login
    Unload UF_Changepass
    Unload Me
End If

这就是我不想做的事。登录,注销,更改通行证和退出 但正如我所说,表格在第一次执行后冻结。

示例:
1.我初始化UF_Login,然后出现UF_Encoding 它有效,所有命令按钮和文本框都有效 3.然后我使用列表框注销 4.当我再次登录时,它将显示UF_Encoding,但是当我尝试使用commanb按钮和文本框时,它不起作用。
奇怪的是,列表框中有注销,更改传递和退出工作。

我真的很难弄清楚原因 任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

试一试。它是“粗糙”的代码,当然需要精炼,但它有效,并且应该给你一些想法。

这是UF_Encoding

Option Explicit
'UF_Encoding form
Private msLBox As String
Private msUser As String
Public Property Let psUser(s As String)
    msUser = s
End Property
Public Property Get psLBox() As String
    psLBox = msLBox
End Property

Private Sub LB_Options_AfterUpdate()
msLBox = Me.LB_Options.Value

If Me.LB_Options.Value = "Log out" Then
    Me.Hide
    Me.LB_Options.Visible = False
    UF_Login.TB_Username.Value = ""
    UF_Login.TB_Password.Value = ""

ElseIf Me.LB_Options.Value = "Change Password" Then
    Me.Hide
    Me.LB_Options.Visible = False
    UF_Changepass.TB_User.Value = ""
    UF_Changepass.TB_Newpass.Value = ""
    UF_Changepass.TB_Oldpass.Value = ""

ElseIf Me.LB_Options.Value = "Exit" Then
    Me.Hide
    '    wbDbase.Save
    '    wbDbase.Close
    '    wbEncoding.Save
    '    wbEncoding.Close
    '    Unload UF_Login
    '    Unload UF_Changepass
    '    Unload Me
    End If
End Sub

Private Sub UserForm_Activate()
    Me.L_User.Caption = "Welcome " & msUser & "!" & " You are logged in."
    Me.TB_Operator.Text = msUser
    msLBox = "" 'reset each time form re-entered
    Me.LB_Options.Visible = True
End Sub

Private Sub UserForm_Initialize()
    Me.LB_Options.AddItem "Log out"
    Me.LB_Options.AddItem "Change Password"
    Me.LB_Options.AddItem "Exit"

    Me.TB_ESN_IMEI.Value = ""
    Me.CB_PrimaryCode.Value = ""
    Me.CB_SecondaryCode.Value = ""
    Me.TB_Remarks.Value = ""
    Me.TB_ESN_IMEI.SetFocus
End Sub

这在UF_Login

Option Explicit
'UF_Login form
Private msUser As String
Public Property Get psUser() As String
    psUser = msUser
End Property

Private Sub CBu_Login_Click()
    Dim ws As Worksheet, rng As Range, lrow As Long, find_value As String
    Dim cel As Range

    Set ws = ThisWorkbook.Sheets("UserName")
    lrow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    Set rng = ws.Range("A2:A" & lrow)
    find_value = Me.TB_Username.Value
    Set cel = rng.Find(What:=find_value, After:=ws.Range("A2"), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    If Not cel Is Nothing Then
        If Me.TB_Password.Value = cel.Offset(0, 1).Value Then
            msUser = cel.Offset(0, 2).Value 'save user name
            Me.Hide
        Else
            MsgBox "Invalid Username/Password"
        End If
    Else
        MsgBox "Invalid Username/Password"
    End If

End Sub

这是UF_Changepass

Option Explicit
'UF_Changepass form
Private Sub CMDok_Click()
    Dim ws As Worksheet, rng As Range
    Set ws = ThisWorkbook.Sheets("UserName")
    ws.Columns("B:B").Find(Me.TB_Oldpass, , xlValues, xlWhole, , , False).Value = Me.TB_Newpass
    Me.Hide
End Sub

Private Sub UserForm_Click()

此代码进入常规模块

Option Explicit
    Dim fLogin As UF_Login
    Dim fEnc As UF_Encoding
    Dim fChg As UF_Changepass

Sub main()
    Dim s As String
'   initialize all 3 forms
    Set fLogin = New UF_Login
    Set fEnc = New UF_Encoding
    Set fChg = New UF_Changepass

    fLogin.Show '1st time

'   re-display main form until done
    Do
        fEnc.psUser = fLogin.psUser 'pass user name to main form
        fEnc.Show
        s = fEnc.psLBox 'get listbox value
        If s <> "Exit" Then showAuxForms s
    Loop Until s = "Exit"

'   done with forms
    Unload fLogin
    Unload fEnc
    Unload fChg
    Set fLogin = Nothing
    Set fEnc = Nothing
    Set fChg = Nothing
End Sub

Sub showAuxForms(s As String)
    If s = "Log out" Then
        fLogin.Show
    ElseIf s = "Change Password" Then
        fChg.Show
    End If
End Sub

答案 1 :(得分:0)

要允许UserForms之间的多个切换,最好卸载您不使用的表单,然后在需要再次使用时重新加载它。类似的东西:

Me.Hide '/* hide the initiating form first */
Load UF_Login '/* loads the form, but not shown */
With UF_Login
    .TB_Username.Value = ""
    .TB_Password.Value = ""
    .Show
End With
Unload Me '/* unload the initiating form */

UF_Login中,系统会添加一个查看UF_Encoding的代码,使其看起来像是在实际登录和退出表单。

Private Sub CB_Login_Click()

    '/* code to check log-in credentials goes here */ 
    Me.Hide
    Load UF_Encoding
    UF_Encoding.Show
    Unload Me
End Sub