运行时错误:无法获取WorksheetFunction类的Vlookup属性

时间:2016-02-28 14:32:30

标签: excel excel-vba vlookup excel-2013 vba

我正在尝试在vba上创建登录用户表单,但它会返回标题中声明的错误。所以这里有一些背景知识。

我有一张名为“User_List”的工作表,其中包含用户名及其相关密码。范围从范围B3到C1000。 B列具有所有用户名,而C列具有密码。我想创建一个登录用户表单,以便在用户输入用户名和密码后,vba将搜索用户列表并确定详细信息是否正确。一旦确认,vba将把用户引导到另一张名为主页的表格。以下是我的代码。

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

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

1 个答案:

答案 0 :(得分:3)

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

首先,检查列B中提供的用户名是否存在快速MATCH function。如果存在,请检查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(默认情况下)区分大小写,因此当用户名查找不区分大小写时,密码检查区分大小写。这很可能是你想要的。