我认为这个问题已被问过一千次,但经过几个小时的搜索,我仍无法找到任何帮助。因此,我需要使用数字向量作为输入,对所有这些数字进行计算,然后将结果作为具有与输入向量相同维度的向量返回。所以,我想说我想取一些数字,然后将它们全部减去一个,所以我的Excel表格看起来应该是这样的
A B
1 6 5
2 7 6
3 8 7
4 9 8
5 10 9
其中列A是输入向量,B应该是输出。但我的输出只是一列零。调试还告诉输出向量中有正确的数字,但我只能打印第一个值然后转到所有单元格。目前我的代码看起来像这样
Public Function Vector(values As Range) As Variant
Dim arraySize As Integer, i As Integer
arraySize = values.rows.Count
Dim returnArray() As Variant
ReDim returnArray(arraySize)
For i = 1 To arraySize
returnArray(i - 1) = values(i, 1) - 1
Next i
Vector = returnArray
End Function
答案 0 :(得分:2)
使用二维数组:
Public Function Vector(values As Range) As Variant
Dim arraySize As Integer, i As Integer
arraySize = values.Rows.Count
Dim returnArray() As Variant
ReDim returnArray(1 To arraySize, 1 To 1)
For i = 1 To arraySize
returnArray(i, 1) = values(i, 1) - 1
Next i
Vector = returnArray
End Function
注意:强>
在工作表中输入数组公式时,点亮单元格 B1 到 B5 ,然后在公式栏中单击,然后使用 Ctrl输入公式 + Shift + 输入
答案 1 :(得分:0)
Public Function Vector(values As Range) As Variant
Dim r As Long, c As Long, returnArray As Variant
returnArray = values.Value
For r = 1 To ubound(returnArray, 1)
For c = 1 To ubound(returnArray, 2)
returnArray(r, c) = returnArray(r, c) - 1
Next c
Next r
Vector = returnArray
End Function