我在尝试着
- 将两个范围 -多行单列- 传递给 Excel 2007 中的用户定义函数,
- 然后将其分配给数组进行处理。
谁能告诉我如何将这样的范围分配给数组?
范围不是恒定的,因为我在不同的单元格中为不同的数据使用 UDF,所以我不能使用例如,Range("A1:A10")
当我只使用Data1.Rows.Cells(i, 1)
而不是数组时,代码正在工作。但我认为最好使用一维数组来提高效率。
这是我当前的代码
Function Sample(Data1 As Range, Data2 As Range) As Double
'Size of Data1 and Data2
Dim rows As Integer
rows = Data1.Rows.Count
'Declaring two one dimensional arrays
Dim data1Array(rows) As Double --- Getting error here
Dim data2Array(rows) As Double --- Getting error here
Dim diff As Double
Dim average As Double
Dim i As Integer
'Assigning Range to Array
data1Array = Data1 --- Getting Error here
data2Array = Data2 --- Getting Error here
average = 0
diff = 0
For i = 1 To rows
diff = data1Array(i) - data2Array(i)
If diff < 0 Then
diff = diff * -1
End If
average = diff + average
Next i
Sample = average/rows
End Function