初始化时清除用户窗体

Clear Userform when initializing

我有一个系统,当复选框被选中时,它会保存用户名和密码信息,但未选中它应该清除密码。

在某些情况下,当我打开表单时它可以工作,如果我调试并逐行进行,这就可以工作,但它几乎就像 userform/excel 有一个缓存,不会每次都清除它。

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

当您使用Login.Hide时,用户窗体被隐藏,但窗体中的数据仍然存在。请尝试使用 Unload Me