我正在创建用户定义的函数并从一系列单元格中输入值(例如= sk_test(A1:a5)),使用这些值进行计算,返回一个包含单元格值的数组。要使用它,我突出显示将保存返回值的单元格,输入函数调用(如= sk_test(A1:a5))和CTRL + SHIFT + RETURN。以下代码给出了错误。感谢。
此致 史蒂夫
Function sk_test(data1 As Range) As Variant
Dim c As Range
Dim aa() As Double
Dim i, npt As Integer
Dim data2 As Variant
data2 = data1
npt = UBound(data2)
Redim aa(1 To npt)
i = 1
For Each c In data1
aa(i) = c.Value * 10 + 99 ' example of calcuations
i = i + 1
Next c
i = 1
For Each c In data2
c.Value = aa(i)
i = i + 1
Next c
sk_test = data2
End Function
答案 0 :(得分:1)
只需替换
i = 1
For Each c In data2
c.Value = aa(i)
i = i + 1
Next c
sk_test = data2
通过
sk_test = Application.Transpose(aa)
答案 1 :(得分:0)
这就是我总是创建一个函数而不是返回一个数组
Public Function Sequence(ByVal N As Long, ByVal M As Long) As Variant()
Dim res() As Variant
Dim i As Long, j As Long
' Set return shape
ReDim res(1 To N, 1 To M)
' Sample calculation
For i = 1 To N
For j = 1 To M
res(i, j) = (i - 1) * M + j
Next j
Next i
' Return array
Sequence = res
End Function