Run-time Error : Unable to get Vlookup property of the WorksheetFunction Class

Run-time Error : Unable to get Vlookup property of the WorksheetFunction Class

我正在尝试在 vba 上创建一个登录用户表单,但 returns 我遇到了标题中所述的错误。所以这里有一些背景。

我有一个名为 "User_List" 的 sheet,其中包含用户名及其关联的密码。范围从范围 B3 到 C1000。 B 列包含所有用户名,而 C 列包含密码。我想创建一个登录用户表单,以便在用户输入用户名和密码后,vba 将搜索用户列表并确定详细信息是否正确。确认后,vba 会将用户定向到另一个名为主页的 sheet。下面是我的代码。

Private Sub MLIB_Click()
Dim UserName As String
Dim PassWord As String

' MUN is the name of the textbox associated to the Username
' MPW is the name of the textbox associated to the Password
UserName = MUN.Value
PassWord = MPW.Value

If UserName = Application.WorksheetFunction.VLookup(UserName, Sheets("User_List").Range("B3:C1000"), 1, True) Then
    If PassWord = Application.WorksheetFunction.VLookup(PassWord, Sheets("User_List").Range("B3:C1000"), 2, True) Then
       Sheets("Home_Page").Activate
       Unload Me
    End If
End If
MsgBox "Sorry, Incorrect Login Details"
End Sub

一直在努力解决这个问题,但需要很长时间!感谢任何帮助!

您应该使用 False 作为 WorksheetFunction object VLOOKUP function 的可选 [range_lookup] 参数与未排序数据完全匹配。

首先,快速MATCH function检查B列中是否存在所提供的用户名。如果存在,请检查 C 列中的关联密码是否与提供的密码相同。

With Worksheets("User_List")
    If Not IsError(Application.Match(UserName, .Columns(2), 0)) Then
        If Password = Application.VLookup(UserName, .Range("B:C"), 2, False) Then
           Worksheets("Home_Page").Activate
           Unload Me
        End If
    End If
End With
MsgBox "Sorry, Incorrect Login Details"

VBA(默认情况下)区分大小写,因此虽然用户名查找不区分大小写,但密码检查区分大小写。这可能是您想要的。