2016-03-16 199 views
2

我試圖做一個自動化vlookupfunction,但我得到一個運行時錯誤:VLOOKUP用VBA

1004 "Unable to get the Vlookup property of the worksheet functionclass" 

那就需要放在其出了錯一些幫助和如何可以調整!

這是代碼:

Sub FINDSAL() 
'On Error GoTo MyErrorHandler: 
Dim Seat_No As String 
Seat_No = InputBox("Enter the Seat Number:") 
If Len(Seat_No) > 0 Then 
    nameit = Application.WorksheetFunction.Vlookup(Seat_No, Sheets("L12 - Data Sheet").Range("B4:E250"), 2, False) 
    MsgBox "The name is : $ " & nameit 
Else 
    MsgBox ("You entered an invalid value") 
End If 
Exit Sub 
MyErrorHandler: 
If Err.Number = 1004 Then 
    MsgBox "Employee Not Present in the table." 
End If 
End Sub 
+1

錯誤1004通常意味着VLOOKUP返回#NA。你有沒有嘗試解除這條線的註釋:'On Error GoTo MyErrorHandler: – bioschaf

+0

是的,我做了,它說員工不在場,但它確實存在於數據表中:/ @bioschaf – Niva

+0

並且爲了添加到@bioschaf,我的猜測是' Seat_No'是一個字符串,但表中的座位號是一個整數,請嘗試'Application.WorksheetFunction.Vlookup(Int(Seat_No),...'。更一般地,在VLookup上設置一個斷點並查看會發生什麼。 – stephan

回答

0
Sub FINDSAL() 

On Error GoTo MyErrorHandler: 
Dim Seat_No As Integer 
Seat_No = InputBox("Enter the Seat Number:") 
If Len(Seat_No) > 0 Then 
    nameit = Application.WorksheetFunction.Vlookup(Seat_No, Sheets("L12 - Data Sheet").Range("B4:E250"), 2, False) 
    MsgBox "The name is : $ " & nameit 
Else 
    MsgBox ("You entered an invalid value") 
End If 
Exit Sub 
MyErrorHandler: 
If Err.Number = 1004 Then 
    MsgBox "Employee Not Present in the table." 
End If 
End Sub