我正在写一个公式,用于在单元格中对数据列进行计算,具体取决于它在列标题(用作参数的范围)下看到的内容。该代码在vba中运行,但返回#VALUE!在单元格中。我的问题归结为在单元格中使用错误的语法将范围传递给函数。
函数是
Function MultiComp(Date1 As Range, Delta1 As Range, Date2 As Range, Delta2 As Range)
'Do Stuff
'Return answer as a double or "Insufficient" if no data is available
End Function
并且在以下行中被另一个函数调用时正在VBA中运行:
Range("K25") = MultiComp(Range("B4"), Range("F4"), Range("M4"), Range("Q4"))
但显示#VALUE!在单元格中使用时:
"=MultiComp(B4, F4, M4, Q4)"
这应该是一个相对简单的语法修复程序,但是对我而言,我无法将其固定下来。
我尝试了Range(B4),Range(“ B4”),B4:B4的变体,尝试使用间接和下面的代码作为受控示例,其行为与预期的“ = RangeTest(J20) ”。
Function RangeTest(Here As Range)
MsgBox (Here.Address & " is range Address" & vbNewLine & Here & " is range Value")
End Function
编辑:完整代码为
Function MultiComp(Date1 As Range, Delta1 As Range, Date2 As Range, Delta2 As Range)
Dim wsf As WorksheetFunction
Dim ws As Worksheet
Dim V1w, V2w As Variant
Set wsf = Application.WorksheetFunction
Set ws = ThisWorkbook.Worksheets("TestingPage")
A = FindRecentDates(Date1)
V1 = wsf.Transpose(RangeFromTop(Delta1, A).Value)
ReDim V1w(1 To UBound(V1))
For i = 1 To UBound(V1w)
V1w(i) = ws.Range("A1") - (wsf.Days(Now(), Date1.Offset(i - 1, 0)))
Next i
V1SumP = wsf.SumProduct(V1, V1w)
V1Weight = wsf.Sum(V1w)
TotalSumP = TotalSumP + V1SumP
TotalWeight = TotalWeight + V1Weight
B = FindRecentDates(Date2)
V2 = wsf.Transpose(RangeFromTop(Delta2, B).Value)
ReDim V2w(1 To UBound(V2))
For i = 1 To UBound(V2w)
V2w(i) = ws.Range("A1") - (wsf.Days(Now(), Date2.Offset(i - 1, 0)))
Next i
V2SumP = wsf.SumProduct(V2, V2w)
V2Weight = wsf.Sum(V2w)
TotalSumP = TotalSumP + V2SumP
TotalWeight = TotalWeight + V2Weight
'/Calculating Average/
If TotalWeight = 0 Then TotalWeight = 1
WeightedDelta = (TotalSumP / TotalWeight) '/ Denominator
MultiComp = UndoDelta(WeightedDelta)
If TotalSumP = 0 Then MultiComp = "Calculating"
End Function
哪个本身正在调用其他函数,但我不确定这是否足以满足要求。