[英]Excel VBA - Custom Function; #VALUE error; VLOOKUP on different worksheet
我正在嘗試根據函數中的給定參數在其他工作表上執行VLOOKUP。 我已經玩了幾個小時,無法弄清楚為什么它不起作用。 我盡力減少了測試代碼,但無法有效地找到解決方案。 我認為這可能是我如何從VLOOKUP的其他工作表中調用范圍的問題。 代碼如下。 請指教。 如果我不清楚我要問的是什么,那就問一下,我會提供反饋。 謝謝
Function GraphDataA(cR As String, time As String, aClient As String, tps As String, dat As String)
Dim client As Boolean
Dim day As Boolean
Dim tot As Boolean
Dim dayTotData As Range
Dim dayTotDatas As Worksheet
Set dayTotDatas = ActiveWorkbook.Sheets("DayTot")
Set dayTotData = dayTotDatas.Range("A3:AI168")
client = False
day = False
tot = False
If date = "" Then
GraphDataA = ""
End If
If aClient = "" Then
GraphDataA = ""
End If
If cR = "Client" Then
client = True
End If
If time = "Day" Then
day = True
End If
If tps = "Total" Then
tot = True
End If
If client = True Then
If day = True Then
If tot = True Then
GraphDataA = WorksheetFunction.VLookup(aClient, dayTotData, WorksheetFunction.Match(dat, dayDate, 0) + 8, _
False)
End If
End If
End If
End Function
如果沒有匹配項, VLOOKUP()
將引發錯誤。 因此,您需要在函數中添加錯誤捕獲代碼。
您需要將功能修改為
Function MyFunction() as Something
On Error Goto ErrorHandler
' Your existing code goes here
Exit Function
ErrorHandler:
MyFunction = -1 ' Or something which indicates that the value isn't found
End Function
您似乎沒有從函數中返回任何值。 嘗試將As Variant
添加到第一行的末尾,如下所示:
Function GraphDataA(cR As String, time As String, aClient As String, tps As String, dat As String) As Variant
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.