通过UDF返回一个数组

我有两个macros。 其中之一创build一个基于范围的数组。 第二个将采取该arrays,通过它,并删除任何重复的条目。 然后我想要第二个macros返回一个数组,我可以继续在第一个macros中使用。

以下是我迄今为止所尝试的:

这是获得数组:

Sub array_Test() Dim array1() As Variant ReDim array1(1 To 10) array1 = Range("A1:A10") GEN_USE_Remove_Duplicates_from_Array (array1) Dim i As Integer Debug.Print Join(array1, "/") 'Now that array1 has had duplicates removed, print the remaining numbers. This is where I don't know what to do For i = 0 To UBound(array1) Range(Cells(i + 1, 2).Value) = array1(i + 1) Next i End Sub 

这是GEN_USE_Remove_Duplicates … sub:

 Sub GEN_USE_Remove_Duplicates_from_Array(arr As Variant) Dim Array_1 Array_1 = arr Dim Array_2() Dim Array_toRemove() Dim dic As New Scripting.Dictionary Dim arrItem, x As Long For Each arrItem In Array_1 If Not dic.Exists(arrItem) Then dic.Add arrItem, arrItem End If Next Array_2 = dic.Keys Debug.Print Join(Array_2, "/") GEN_USE_Remove_Duplicates_from_Array = Array_2 End Sub 

那成功地保留唯一的价值。 现在,我怎么得到这个结果(Array_2)在array_Test子使用? 我需要创build一个function,而不是一个小组?

非常感谢任何build议/帮助/提示!

macrosSub不会返回一个值,但你可以很容易地将其交换到一个函数。

 function GEN_USE_Remove_Duplicates_from_Array(arr As Variant) ' lots of code stuff here ... GEN_USE_Remove_Duplicates_from_Array = Array_2 end function 

将返回的数组分配给一个数组,

 array1 = GEN_USE_Remove_Duplicates_from_Array(array1) 

fww,你应该可以把它作为一个子和使用,

 arr = array2 

这也应该改变作为单个parameter passing给数组的参数。