'第一步工作簿的打开和关闭设置:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Sheets("空白").Visible = xlSheetVisible
For Each ws In Worksheets
If ws.Name <> "空白" Then
ws.Visible = xlSheetVeryHidden
End If
Next ws
ThisWorkbook.Save
End Sub
-------------------------------------------------------------------
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In Worksheets
If ws.Name <> "注册" Then
ws.Visible = xlSheetVisible
End If
Next ws
Sheets("空白").Visible = xlSheetVeryHidden
Application.EnableCancelKey = xlDisabl '禁用Ctrl+break键
Application.Visible = False
UserForm1.Show 0
End Sub
'第二步,登录窗体设置:
Private Sub CommandButton1_Click()
Unload Me
ThisWorkbook.Save
ThisWorkbook.Close
End Sub
Private Sub CommandButton2_Click()
If Len(UserForm1.user) = 0 Then MsgBox "未输入账号": Exit Sub
Set zh = Sheets("注册").Range("A:A").Find(UserForm1.user.Text, , , 1)
If Not zh Is Nothing Then
If psw.Text <> zh.Offset(0, 1) Then
MsgBox "密码错误"
Else
MsgBox "登录成功"
Unload Me
Application.Visible = True
End If
Else
MsgBox "账号不存在"
End If
End Sub
Private Sub Label3_Click()
UserForm2.Show
End Sub
Private Sub Label4_Click()
UserForm3.Show
End Sub
Private Sub UserForm_Initialize()
Caption = "今天是:" & Format(Now(), "YYYY年mm月dd日 HH:MM aaaa") & " 第" & WorksheetFunction.WeekNum(Now, vbMonday) & "周"
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> 1 Then Cancel = True '禁止用右上角关闭按钮
End Sub
'第三步,注册窗体设置:
Private Sub CommandButton1_Click()
Dim zh As Range, zt As Range, zc As Range, xz As Range
Dim reg As Object
If UserForm2.TextBox4 = "" Then MsgBox "未填入注册员工姓名": Exit Sub
If UserForm2.TextBox1 = "" Then MsgBox "未填入账号": Exit Sub
If UserForm2.TextBox2 <> "" And UserForm2.TextBox2 <> UserForm2.TextBox3 Then MsgBox "密码不一致": Exit Sub
Set reg = CreateObject("vbscript.regexp")
Set zh = Sheets("注册").Range("A:A").Find(UserForm2.TextBox1.Text, , , 1)
Set zc = Sheets("注册").Range("H:H").Find(UserForm2.TextBox4.Text, , , 1)
Set xz = Sheets("注册").Range("c:c").Find(UserForm2.TextBox4.Text, , , 1)
For Each ze In Array("[A-Z]", "[a-z]", "\d")
With reg
.Global = True
.Pattern = ze
If .test(UserForm2.TextBox2.Text) Then
n = n + 1
End If
End With
Next ze
If n < 3 Then MsgBox "密码中没有包含大写字母,小写字母,数字": Exit Sub
If zh Is Nothing Then
If xz Is Nothing Then
If Not zc Is Nothing Then
Set zt = Sheets("注册").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
zt = UserForm2.TextBox1.Text
zt.Offset(0, 1) = UserForm2.TextBox2.Text
zt.Offset(0, 2) = UserForm2.TextBox4.Text
zt.Offset(0, 3) = Now
MsgBox "注册成功"
Unload Me
Else
MsgBox "注册员工不存在,请更换其他员工"
End If
Else
MsgBox "注册人已存在,请更换其他员工"
End If
Else
MsgBox "账号已存在,请更换其他账号"
End If
End Sub
'第四步,注册窗体设置:
Private Sub CommandButton1_Click()
If UserForm3.TextBox1 = "" Then MsgBox "请输入账号": Exit Sub
If UserForm3.TextBox2 = "" Then MsgBox "请输入原密码": Exit Sub
If UserForm3.TextBox3 = "" Then MsgBox "请输入新密码": Exit Sub
Dim zh As Range
Set zh = Sheets("注册").Range("A:A").Find(UserForm3.TextBox1.Text, , , 1)
If Not zh Is Nothing Then
If zh.Offset(0, 1) = UserForm3.TextBox2.Text Then
zh.Offset(0, 1) = UserForm3.TextBox3.Text
Unload Me
Else
MsgBox "密码输入错误"
End If
Else
MsgBox "你输入的账号不存在"
End If
End Sub