Excel VBA用户名/密码查找

Private Sub cmdLogin_Click() On Error GoTo ErrorHandler Dim RowNo As Long Dim Id As String Dim pw As String Dim ws As Worksheets Application.ScreenUpdating = False Set ws = Worksheets("User&Pass") Id = LCase(Me.txtLogin) RowNo = Application.WorksheetFunction.Match(Id, ws.range("A2:A999"), 0) CleanExit: Set ws = Nothing ' free memory Application.ScreenUpdating = True ' turn on the screen updating Exit Sub ErrorHandler: MsgBox "Unable to match ID, enter valid ID.", vbOKOnly GoTo CleanExit End Sub 

我有一个excel用户表单,我一直在努力,现在我需要它看起来更专业的login屏幕。 我已经从上面的代码开始,但是我已经走到了死胡同。

如何设置我的目的是说如果ID和密码匹配,然后加载工作簿或取消隐藏工作簿,并继续。 用户名和密码在名为“User&Pass”的工作表上。目的是从a-user / b-pw的列分别读取,如果成功,我将隐藏该表,以便他们不能看到其他用户的信息

与我上面开始我只是需要它说,如果它匹配usercolumn然后相应的pw隔壁它继续否则去我的error handling程序

我可以做隐藏和取消隐藏表格的格式,只需要帮助阅读用户名和密码

非常感谢Z

编辑尝试一个;

 Private Sub cmdLogin_Click() On Error GoTo ErrorHandler Dim RowNo As Long Dim Id As String Dim pw As String Dim ws As Worksheets Application.ScreenUpdating = False Set ws = Worksheets("User&Pass") Id = LCase(Me.txtLogin) RowNo = Application.WorksheetFunction.Match(Id, ws.range("A2:A999"), 0) RowNo = RowNo + 1 pw = ws.range("B" & RowNo) If pw = Me.txtLogin Then 'continue txt1.Value = "yes" Else GoTo ErrorHandler End If CleanExit: Set ws = Nothing ' free memory Application.ScreenUpdating = True ' turn on the screen updating Exit Sub ErrorHandler: MsgBox "Unable to match ID, enter valid ID.", vbOKOnly GoTo CleanExit End Sub 

@siddarthRout

 Private Sub cmdLogin_Click() Dim RowNo As Long Dim Id As String, pw As String Dim ws As Worksheet Dim aCell As range On Error GoTo ErrorHandler Application.ScreenUpdating = True Set ws = Worksheets("Details") Id = LCase(Me.txtLogin) Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) '~~> If match found If Not aCell Is Nothing Then RowNo = aCell.Row '~~> Rest of your code. For example if the password is '~~> Stored in Col B then Debug.Print aCell.Offset(, 1) Unload Me FrmMenu.Show '~~> You can then use the above aCell.Offset(, 1) to '~~> match the password which the user entered Else '<~~ If not found MsgBox "Unable to match ID, enter valid ID.", vbOKOnly End If CleanExit: Set ws = Nothing Application.ScreenUpdating = True Exit Sub ErrorHandler: MsgBox Err.Description Resume CleanExit End Sub 

testing和试用

这是你正在尝试?

 Option Explicit Private Sub cmdLogin_Click() Dim RowNo As Long Dim Id As String, pw As String Dim ws As Worksheet Dim aCell As Range On Error GoTo ErrorHandler If Len(Trim(txtLogin)) = 0 Then txtLogin.SetFocus MsgBox "Username cannot be empty" Exit Sub End If If Len(Trim(txtPassword)) = 0 Then txtPassword.SetFocus MsgBox "Password cannot be empty" Exit Sub End If Application.ScreenUpdating = False Set ws = Worksheets("User&Pass") Id = LCase(Me.txtLogin) Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) '~~> If match found If Not aCell Is Nothing Then RowNo = aCell.Row If Me.txtPassword = aCell.Offset(, 1) Then FrmMenu.Show Unload Me Else MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly End If Else '<~~ If not found MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly End If CleanExit: Set ws = Nothing Application.ScreenUpdating = True Exit Sub ErrorHandler: MsgBox Err.Description Resume CleanExit End Sub 

提示

永远不要让你的用户知道(从安全angular度)什么是不正确的 – 用户名或密码。 总是显示一个通用的消息,如“无法匹配UserID或PasswordID,请再试一次” 🙂

HTH

希德

其他方式

 On Error Resume Next If Me.password <> Application.VLookup(Me.username, Sheet1.Cells(1, 1).CurrentRegion, 2, False) Then MsgBox ("incorrect") Exit Sub Else MsgBox ("Correct Password Entered") End If 

此外,您需要确保所有工作表从一开始就是xlSheetVeryHidden,以便禁用macros并将其作为成功login例程的一部分而取消隐藏。 您还需要在VBA项目上设置密码,以防止人员取消隐藏表单。 但请记住,Excel和湿纸袋一样安全;)