简体   繁体   English

Excel vba用户名/密码查找

[英]Excel vba username/ password lookup

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

I've got an excel userform i've been working on and now I need it to look more professional by having a log-in screen. 我有一个excel userform我一直在努力,现在我需要它通过登录屏幕看起来更专业。 I've started with the code above but I have come to a dead end. 我已经开始使用上面的代码,但我已经走到了尽头。

how its set up my aim is to say if id & password matches then load up workbook or unhide the workbook and continue. 如何设置我的目标是说id和密码是否匹配然后加载工作簿或取消隐藏工作簿并继续。 the username and password are on a sheet called "User&Pass" Aim is it reads from there in columns a- user / b- pw respectively and if it's a success I will hide that sheet so they cant see other user's information 用户名和密码位于名为“User&Pass”的工作表上。目的是分别从a-user / b-pw列中读取,如果成功,我将隐藏该工作表,以便他们无法看到其他用户的信息

with what I started above I just need it to say if it matches usercolumn then corresponding pw next door to it continue else go to my errorhandler 我从上面开始我只需要它说如果它匹配usercolumn然后相应的pw隔壁继续其他去我的错误处理程序

i can do the formatting about hiding and unhiding sheets etc just need help with reading username and pw 我可以做关于隐藏和取消隐藏工作表的格式等,只需要帮助阅读用户名和密码

thanks very much in advance Z 非常感谢提前Z.

Editted attempt one; 编辑尝试一个;

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 @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

TESTED AND TRIED 测试和尝试

Is this what you are trying? 这是你在尝试什么?

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

TIP : 提示

Never let your user know (from security perspective) what was incorrect - The username or the password. 永远不要让您的用户(从安全角度)知道什么是错误的 - 用户名或密码。 Always show a generic message like "Unable to match UserID or PasswordID, Please try again" :) 始终显示一条通用消息,例如“无法匹配UserID或PasswordID,请再试一次” :)

HTH HTH

Sid 希德

Another way 其他方式

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

Also you will need to make sure that all your sheets are xlSheetVeryHidden from the outset to combat having macros disabled and un-hide them as part of your successful log in routine. 此外,您需要确保所有工作表从一开始就是xlSheetVeryHidden,以防止宏被禁用,并在成功登录例程中取消隐藏它们。 You'll also want to set a password on your VBA project to prevent people unhiding the sheets. 您还需要在VBA项目上设置密码,以防止人们取消隐藏工作表。 Bear in mind however, Excel is about as secure as a wet paper bag ;) 但请记住,Excel与湿纸袋一样安全;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM