2016-02-28 52 views
2

我正试图在vba上创建一个登录用户窗体,但它会返回标题中所述的错误。所以这里有一些背景。运行时错误:无法获取WorksheetFunction类的Vlookup属性

我有一张名为“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 

一直试图弄清楚这一点,但它需要很长时间!感谢任何帮助!

回答

3

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

首先,检查提供的用户名是否存在于列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是(默认情况下)区分大小写,所以在用户名查询不区分大小写的情况下,密码检查区分大小写。这可能是你想要的。

+0

它给了我一个错误 - 运行时错误'438'(对象不支持这个属性或方法),它突出显示了下面的代码。 If Not IsError(Application.Match(UserName,.Column(2),0))Then – Stu

+0

我的歉意。这应该是'.Columns(2)',而不是'.Column(2)'。 – Jeeped

+0

它完美的作品!谢谢!! – Stu

相关问题