2016-09-27 70 views
0

enter image description here无法获取工作表中的WorksheetFunction类错误的VLOOKUP财产

Private Sub TextBox2_AfterUpdate() 'Badge Number 
On Error GoTo Err 
Dim tbl As ListObject, fndStr As String 
Set tbl = Sheet9.ListObjects("EmployeeList") 

fndStr = Trim(Me.TextBox2.Value) 
MsgBox fndStr 
If fndStr <> "" Then 
    Me.TextBox3.Value = Application.WorksheetFunction.VLookup(fndStr, tbl, 2, False) '<-- Error Line 
End If 

Exit Sub 

Err: 
MsgBox Err.Description 

End Sub 

我有一个命名为“EmployeeList的”和我使用的证件号码做简单的VLOOKUP,但我得到了不明原因的错误表。我知道以前有类似的问题,但在发布之前我确实阅读过。

正如您可以清楚地看到图像中的表名并输入了值为10的vlookup函数的第一个参数,但它不会返回任何值,但会给出错误。不知道什么是错的。

'I tried this as well 
    Me.TextBox3.Value = Application.WorksheetFunction.VLookup(fndStr, Sheet9.Range("A1:F" & Rows.Count), 2, False) '<-- Error Line 

    'And this 
    Me.TextBox3.Value = Application.WorksheetFunction.VLookup(fndStr, Sheet9.Range("EmployeeList"), 2, False) '<-- Error Line 

而且由于未知原因,我不能这样做

Application.Vlookup as well 

    Like when I do Application.V 

    Vlookup doesn't shows up in the list. 

回答

3

有两个问题。

第一,你试图解决,是你需要一个Range作为Arg2Vlookup。由于您的tblListObject。您可以简单地使用tbl.Range,请参阅ListObject.Range Property

第二个是,Vlookup在一列数字中找不到字符串。你的第一列是一列数字。所以你需要将字符串转换为数字。

Me.TextBox3.Value = Application.WorksheetFunction.VLookup(CDbl(fndStr), tbl.Range, 2, False) 

应该工作。

+0

好,谢谢,我会尝试 – newguy

1

请找到下面的代码,我已经使用评估方法来获得vlookup结果。

Private Sub TextBox2_AfterUpdate() 

Dim fndStr    As String 
On Error GoTo Err_Desc 

     fndStr = Trim(Me.TextBox2.Value) 
     MsgBox fndStr 
     If fndStr <> "" Then 
      '// Using Eval method 
      Me.TextBox3.Value = Evaluate("=VLOOKUP(" & fndStr & ",EmployeeList[#All],2,0)") 
     End If 
     Exit Sub 

Err_Desc: 
MsgBox Err.Description 

End Sub 
+1

感谢两个答案很好地工作。 – newguy