excel VBA自定义函数如何进行数组计算

2024-11-23 18:41:30
推荐回答(2个)
回答1:

function F(rng as range) as String
if rng.count <>5 then F="Err":exit function
dim a(4) as double,b as integer
dim cel as range
for each cel in rng
a(b)=val(cel.value)
b=b+1
next
F = a(0) + a(1)*a(2) + a(3) + a(4)
end function

回答2:

Function F(mYrange As Range)
Dim myArr(1 To 5)
Dim mYr As Range
Dim i As Single
i = 1
For Each mYr In mYrange
myArr(i) = mYr.Value
i = i + 1
Next

F = myArr(1) + myArr(2) * myArr(3) + myArr(4) + myArr(5)

End Function