2016-02-28 54 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

相關問題