VBA新手,请大家指教,不知道哪里有问题,我的自定义函数总是#Value!
Function AddMultiRange(rg1 As Range, rg2 As Range)
Dim i, min, result
min = rg1.Rows.Count
If rg2.Rows.Count < min Then
min = rg2.Rows.Count
End If
result = 0
For i = 0 To min - 1
result = result + CDec(rg1.Offset(i, 0).Value) * CDec(rg2.Offset(i, 0).Value)
Next
AddMultiRange = result
End Function
即使我只是
Function AddMultiRange(rg1 As Range, rg2 As Range)
CDec (rg1.Offset(i, 0).Value) * CDec(rg2.Offset(i, 0).Value)
AddMultiRange = 0
End Function
一样也是#Value!
难道不能强制类型转换?
测试时使用的数据为
H6: =AddMultiRange(D2:D3,E2:E3)
D2: 344
D3: 444
E2: 1
E3: 0
纠结死了,帮忙看下哪里有问题,谢谢了
顺便问一下Excel中本来有没有这个逻辑的内置函数?
------解决方案--------------------------------------------------------
Function AddMultiRange(rg1 As Range, rg2 As Range)
Dim i, min, result
min = rg1.Rows.Count
If rg2.Rows.Count < min Then
min = rg2.Rows.Count
End If
result = 0
For i = 1 To min
result = result + CDec(rg1.Cells(i, 1).Value) * CDec(rg2.Cells(i, 1).Value)
Next
AddMultiRange = result
End Function
------解决方案--------------------------------------------------------
cdec我还真没用过,不过看上去不支持数组的哦...不知道我的理解对不对...