2012-03-15 309 views
3
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用户表单我一直在努力,现在我需要它通过登录屏幕看起来更专业。我已经开始使用上面的代码,但是我已经陷入了死胡同。Excel vba用户名/密码查找

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

与我上面开始我只需要它说,如果它匹配usercolumn那么相应的PW隔壁它继续别人去我的ErrorHandler

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

非常感谢 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 
+0

快速提示:这是否意味着是一个非常安全的密码?特别是以这种方式完成的Excel对此非常不利。 – Gaffi 2012-03-15 13:57:11

+0

如果有一种方法使它看起来熟悉用户名和密码,并确保以不同的方式做到这一点我是所有人的耳朵,我的代码是我尝试过的一个例子,只是尝试调整你放入的东西,但没有运气 – Zenaphor 2012-03-15 13:59:01

+1

为什么不只是有一个授权用户的列表,然后验证登录的Windows用户名对此?这样就不需要用户名和密码 – SWa 2012-03-15 14:01:02

回答

3

检测过,并试图

这是你想什么呢?

CODE

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 

提示

不要让什么是不正确的用户知道(从安全的角度来看) - 用户名或密码。始终显示的通用消息像“无法匹配用户名或PasswordID,请重试” :)

HTH

希德

+0

,提示我错误处理程序,而不是让我卸载进度,以测试它是否进展,我告诉它写入文本框的值为“是”,但它只是发送给我的错误处理程序 – Zenaphor 2012-03-15 14:41:57

+0

@ Zeaphor:请更新您的第一篇文章,确切的代码,你正在使用:) – 2012-03-15 14:44:11

+0

我用你上面提到的,离开它的acell偏移量,因为它是任何旁边它 – Zenaphor 2012-03-15 15:03:07

1

另一种方式

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,以防止宏被禁用,并将其作为成功登录例程的一部分取消隐藏。您还需要在VBA项目上设置密码,以防止人员取消隐藏表单。但请记住,Excel和湿纸袋一样安全;)

+0

是的,我认为以上所有,人们不会试图闯入它,因为它是一个公司的工具,很好将是,只有这样管理者才会认为更有控制力 – Zenaphor 2012-03-16 07:33:19