用户自定义函数返回#值

我遇到过这样的情况,需要我平均化一个Vlookups数组的结果。 我不知道如何用公式来实现这一点,似乎没有人在StackOverflow也没有任何想法。

所以我决定写一个函数来为我做这个工作。 不幸的是它返回“#VALUE!” 错误,我不知道为什么! 使用msgbox进行testing时,该函数可以正常工作。 我在下面注释了我的代码:

Option Explicit Public Function AvgVlookup(Target_Array As String, Lookup_Array As String, Column_Index As Long) As Double Dim Result As Double Dim Total As Double Dim Counter As Long Dim TargetRange As Range Dim LookupRange As Range Dim Cell As Range ' Remove Absolute Indicator Target_Array = Replace(Target_Array, "$", "") Lookup_Array = Replace(Lookup_Array, "$", "") ' Convert String to Range Set TargetRange = Range(Left(Target_Array, InStr(1, Target_Array, ":") - 1), Mid(Target_Array, InStr(1, Target_Array, ":") + 1)) Set LookupRange = Range(Left(Lookup_Array, InStr(1, Lookup_Array, ":") - 1), Mid(Lookup_Array, InStr(1, Lookup_Array, ":") + 1)) ' Set Variables to 0 Counter = 0 Total = 0 ' For each cell in defined array For Each Cell In TargetRange ' Vlookup the cell and save lookup value to Result variable Result = Application.WorksheetFunction.vlookup(Cell, LookupRange, Column_Index, "False") ' Update variables used to calculate average Total = Total + Result Counter = Counter + 1 Next Cell ' Perform calculation AvgVlookup = Total / Counter End Function 

 Sub test() MsgBox AvgVlookup("A5:A8", "G5:H8", 2) End Sub 

有任何想法吗?

谢谢!

两件事情:

首先,你设置范围的方式有点长,可以简单地截断:

 Set TargetRange = Range(Target_Array) 

删除$后无需parsingstring。

其次,如果目标范围中的某个值不在查找范围内,则需要进行错误检查。

整个代码:

 Public Function AvgVlookup(Target_Array As String, Lookup_Array As String, Column_Index As Long) As Double Dim Total As Double Dim Counter As Long Dim TargetRange As Range Dim LookupRange As Range Dim Cell As Range ' Remove Absolute Indicator Target_Array = Replace(Target_Array, "$", "") Lookup_Array = Replace(Lookup_Array, "$", "") ' Convert String to Range Set TargetRange = Range(Target_Array) Set LookupRange = Range(Lookup_Array) ' Set Variables to 0 Counter = 0 Total = 0 ' For each cell in defined array For Each Cell In TargetRange ' Vlookup the cell and save lookup value to Result variable Dim Result Result = Application.VLookup(Cell, LookupRange, Column_Index, "False") If IsNumeric(Result) Then Total = Total + Result Counter = Counter + 1 End If Next Cell ' Perform calculation AvgVlookup = Total / Counter End Function 

使用上面的函数调用工作表,您需要像这样调用它: =AvgVlookup("A5:A8", "G5:H8", 2)

但这不是很有帮助。 如果您将input更改为范围:

 Public Function AvgVlookup(TargetRange As Range, LookupRange As Range, Column_Index As Long) As Double Dim Result As Double Dim Total As Double Dim Counter As Long Dim Cell As Range ' Set Variables to 0 Counter = 0 Total = 0 ' For each cell in defined array For Each Cell In TargetRange ' Vlookup the cell and save lookup value to Result variable Dim t t = Application.VLookup(Cell, LookupRange, Column_Index, "False") If IsNumeric(t) Then Total = Total + t Counter = Counter + 1 End If Next Cell ' Perform calculation AvgVlookup = Total / Counter End Function 

那么你可以简单地=AvgVlookup($A$5:$A$8,$G$5:$H$8,2) 。 这样你可以突出显示正确的范围,它将工作。 当你想input的是一个范围的时候,也很less打字试图把一个string转换成一个范围。

在这里输入图像说明