初始化时用户表单代码未正确清除

时间:2019-03-12 11:42:57

标签: excel vba userform

我很困惑,就像我说我在VBA中编写代码一样,但是我有一个系统,当选中此复选框时,它将保存用户名和密码信息,但如果未选中,则应清除密码。在某些情况下,当我打开表单时,如果我调试并逐行运行,则可以正常运行,但这几乎就像userform / excel的缓存不会每次都将其清除一样。如果有人对如何阻止这种情况有任何想法,请告诉我。谢谢。

How the code should run when checkbox1.value = false

How it runs when checkbox1.value = false

Private Sub UserForm_Initialize()


Dim StoreUsername As String, StorePassword As String, storeCheckbox As String

Application.DisplayFullScreen = True


With Me
   .Width = Application.UsableWidth / 4
   .Height = Application.UsableHeight / 2
End With

With Frame1
   .Width = Application.UsableWidth / 2
   .Height = Application.UsableHeight / 4
End With


TextBox2.Value = ""


TextBox2.PasswordChar = "*"

StoreUsername = Sheets("Settings").Range("A2").Value
StorePassword = Sheets("Settings").Range("B2").Value


If Sheets("Settings").Range("C2").Value = "True" Then

        CheckBox1.Value = True

ElseIf Sheets("Settings").Range("C2").Value = "False" Then

        CheckBox1.Value = False

End If


If CheckBox1.Value = True Then

    TextBox1.Value = StoreUsername
    TextBox2.Value = StorePassword


ElseIf CheckBox1.Value = False Then


    TextBox1.Value = StoreUsername
    TextBox2.Value = ""

End If



End Sub







Private Sub CommandButton1_Click()



Dim username As String, Password As String, R As Range, M As Range
Dim Name As String, SECLEVEL As String, Level As String

Dim StoreUsername As String, StorePassword As String
Dim CheckboxStatus As String




username = Login.TextBox1.Value
Password = Login.TextBox2.Value






Set R = Sheets("Employee").Range("A:A").Find(What:=TextBox1.Text, LookAt:=xlWhole, MatchCase:=False)
Set M = Sheets("Employee").Range("F:F").Find(What:=TextBox2.Text, LookAt:=xlWhole, MatchCase:=False)

'MsgBox r


If R Is Nothing Then

    MsgBox "User is not found"
    Exit Sub


Else

Name = R.Offset(0, 1).Value

End If


SECLEVEL = R.Offset(0, 4).Value

MainMenu.Label3.Caption = "Good Morning " & Name

SuperAdminMainMenu.Label3.Caption = "Good Morning " & Name

'Level = SECLEVEL.Value


If TextBox1.Value = "" Then

    MsgBox "You need to Enter a valid Username"
    Exit Sub

Else


    If TextBox2.Value = "" Then

        MsgBox "You need to Enter a valid Password"
        Exit Sub

    Else

    End If



End If


If R Is Nothing Then


    MsgBox "Please Enter a valid Username"


Else

    If M Is Nothing Then

            MsgBox "Please Enter a valid Username and Password"

    Else





        If R.Offset(0, 4).Value = "Super Admin" Then


             Application.DisplayFullScreen = True

             SuperAdminMainMenu.Label4 = username

             MainMenu.TextBox1.Value = username

                If CheckBox1.Value = True Then

                         Sheets("Settings").Range("C2").Value = "True"
                         Sheets("Settings").Range("A2").Value = username
                         Sheets("Settings").Range("B2").Value = Password

                 ElseIf CheckBox1.Value = False Then

                        Sheets("Settings").Range("C2").Value = "False"
                        Sheets("Settings").Range("A2").Value = username
                        Sheets("Settings").Range("B2").Value = ""

                 End If


                Login.Hide

                SuperAdminMainMenu.Show


        Else


             Application.DisplayFullScreen = True

            MainMenu.TextBox1.Value = username



            If CheckBox1.Value = True Then

                 Sheets("Settings").Range("C2").Value = "True"
                 Sheets("Settings").Range("A2").Value = username
                 Sheets("Settings").Range("B2").Value = Password

            ElseIf CheckBox1.Value = False Then

                 Sheets("Settings").Range("C2").Value = "False"
                 Sheets("Settings").Range("A2").Value = username
                 Sheets("Settings").Range("B2").Value = ""
                 TextBox2.Value = ""

            End If



            Login.Hide

            MainMenu.Show


        End If





    End If






End If







 End Sub

1 个答案:

答案 0 :(得分:1)

使用Login.Hide时,userform被隐藏,但是表单中的数据仍然保留。尝试改用Unload Me