[英]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.