在EXCEL中,打开工作表前会自动弹出一个用户登录权限的对话框,是怎么做出来的Excel也可做出像管理软件一样先要求用户登录,才能进入打开Excel文件内容,这个功能实现了用户登录,密码检验,还带了一个用户注册的功能,值得参考
提供者:王平老师
登录界面:
1480512062714973.jpg
Excel 登录 注册 操作动画教程:
1480512117476318.gif
Excel 登录 注册 完整的VBA代码:
Private Sub Workbook_Open() Application.Visible = False 系统登陆.Show
End Sub
Function 取指定用户密码(x As Object) Dim mrow As Integer mrow = Sheets("用户及密码").Cells.Find(x.Text).Row 取指定用户密码 = Sheets("用户及密码").Cells(mrow, 2)End Function
Private Sub CommandButton1_Click() If ComboBox1.Text = "" Or TextBox1.Text = "" Then MsgBox "请填写齐全", 1 + 64, "系统登陆" Else If 取指定用户密码(ComboBox1) = TextBox1.Text Then Unload Me MsgBox ComboBox1.Text & " 你好!欢迎你进入本系统", 1 + 64, "欢迎词" Application.Visible = True Else MsgBox "登陆密码错误,请重新输入" End If End IfEnd SubPrivate Sub CommandButton3_Click() Dim x As String, y As String, z As String MsgBox "使用前注册机会只有一次,请仔细输入并记着用户名及密码!", 1 + 64, "温馨提示" Application.Visible = Trueok: If Sheets("用户及密码").Range("C2") = "" Then x = InputBox("请输入你想要注册的用户名", "注册") Sheets("用户及密码").Range("A65536").End(3).Offset(1, 0) = x y = InputBox("请输入密码", "注册") z = InputBox("请再次输入密码", "注册") If y = z Then Sheets("用户及密码").Range("B65536").End(3).Offset(1, 0) = y Else MsgBox "二次密码不一致,请重新注册!" GoTo ok End If MsgBox "注册成功!请牢记你刚才输入的用户名及密码。" Sheets("用户及密码").Range("C2") = "已经注册过一次" If Sheets("用户及密码").Range("b65536").End(3) = "" Then Sheets("用户及密码").Range("b65536").End(3).Offset(0, -1) = "" Else MsgBox "已经注册过一次,如果不能登陆请找管理员", 1 + 64, "警告!" Unload Me Application.Visible = False ActiveWorkbook.Close End If Unload Me MsgBox " 你好!欢迎你进入本系统", 1 + 64, "欢迎词" Application.Visible = TrueEnd SubPrivate Sub CommandButton2_Click() Unload Me Application.Visible = False ActiveWorkbook.CloseEnd SubPrivate Sub UserForm_Initialize() Dim x As Integer, y As Integer x = Sheets("用户及密码").Range("A65536").End(xlUp).Row For y = 2 To x ComboBox1.AddItem Sheets("用户及密码").Cells(y, 1) Next yEnd SubPrivate Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer) If CloseMode = 0 Then Cancel = 1End Sub |