核心代码:
' 窗口事件
Private Sub CommandButton1_Click() ' 登录
If ComboBox2.Text = "" Or TextBox1.Text = "" Then
MsgBox "请输入账户或密码", 1 + 64, "系统登录"
Else
If 特定用户密码登录(ComboBox2) = TextBox1.Text Then
Unload Me
MsgBox ComboBox2.Text & "你好!欢迎你进入本系统", 1 + 64, "欢迎词"
Application.Visible = True
Else
MsgBox "登录密码错误,请重新输入"
End If
End If
End Sub
Private Sub CommandButton2_Click() ' 退出
Unload Me
Application.Visible = True
ActiveWorkbook.Close
End Sub
Private Sub UserForm_Initialize() ' 窗口初始化
Dim X As Integer, Y As Integer
X = Sheets("用户及密码").Range("A65536").End(xlUp).Row
For Y = 2 To X
ComboBox2.AddItem Sheets("用户及密码").Cells(Y, 1)
Next Y
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) ' 设置不能通过关闭符号退出,仅能通过退出按钮退出
If CloseMode = 0 Then Cancel = 1
End Sub


' 模块
Function 特定用户密码登录(X As Object)
Dim MROW As Integer
MROW = Sheets("用户及密码").Cells.Find(X.Text).Row
特定用户密码登录 = Sheets("用户及密码").Cells(MROW, 2)
End Function


本文介绍如何利用VBA(Visual Basic for Applications)在Excel中实现窗口登录功能,通过编写宏代码来创建用户输入验证,提升工作簿的安全性和交互体验。
3040

被折叠的 条评论
为什么被折叠?



