从工作表Excel VBA中的用户名和密码登录

时间:2017-11-03 19:55:35

标签: vba excel-vba excel

我正在尝试在Excel VBA中创建一个登录表单,该表单从工作簿中的工作表中读取用户名和密码。我已经创建了一个包含多个用户的登录框但我希望能够输入新的用户名和密码,然后使用它登录而无需输入更多代码。非常感谢任何帮助,谢谢。 这是我到目前为止: Usernames and passwords code

2 个答案:

答案 0 :(得分:0)

也许您需要使用命名范围或表来保存用户名和密码,然后使用

进行评估
If Not IsError(Application.Match(Username/Password, Rangename/table, 0)) Then

当找到用户名或密码时,这将评估为真。

如果不是IsError(Application.Match(用户名,用户名范围,0))那么

答案 1 :(得分:-1)

Sub LoopingWorksWell()

Dim c, rng as Range
Dim strUser, strPW as String
Dim shtData as Worksheet, shtEntry as Worksheet
Dim bFound as Boolean

Set shtEntry = ThisWorkbook.Sheets("Sheet1") 'or wherever they are entering UN/PW
Set shtData = ThisWorkbook.Sheets("Sheet2") 'or whatever sheet contains pws
Set rng = sht.Range("A:A") 'or whatever range contains Users, PWs should be the adjacent column

bFound = False

    strUser = shtEntry.Range("A1") 'or wherever
    strPW = shtEntry.Range("A2") 'or wherever
        For each c in rng
             If strComp(c, strUser, vbTextCompare) = 0 and _
                strComp(c.Offset(0,1), strPW, vbBinaryCompare) = 0 Then
                   'Whatever you want your code to do
                bFound = True
                Exit For
             End If
        Next c

    If bFound = False Then MsgBox "Incorrect User Name or Password"

End Sub