为什么是从函数返回后不保存数据的引用值

时间:2014-09-07 18:43:15

标签: excel vba reference

我有一个函数,并通过引用为该函数赋予变量。只要我在函数中,veriable保存我期望的数据,但是从函数返回后,引用的值确实包含了一些奇怪的数据......我没有在函数中看到错误...可以谁有人告诉我这个问题?调试输出显示问题... X2和X3中的组值应匹配,但它们不匹配...

这里我称之为主要功能:

Dim groupVar As Integer
Dim variationGroups() As Integer    

Call GetUniqueVariationGroups(variationGroups)
For countVarGroup = 1 To UBound(variationGroups)
    Debug.Print "X3: index = " & countVarGroup & " | group = " & variationGroups(countVarGroup)
Next countVarGroup

主要功能如下(顺便说一句,variation是自定义类,variation(i).groupInteger

Public Function GetUniqueVariationGroups(ByRef groups() As Integer)

Dim i As Integer
Dim j As Integer
Dim exists As Boolean

For i = 1 To UBound(variation)
    Debug.Print "X1: " & variation(i).group
Next i

ReDim groups(0)

For i = 1 To UBound(variation)

    exists = False
    For j = 1 To UBound(groups)
        If variation(i).group = groups(j) Then
            exists = True
            Exit For
        End If
    Next j

    If Not exists Then
        ReDim groups(UBound(groups) + 1)
        groups(UBound(groups)) = variation(i).group
        Debug.Print "X2: size/index = " & UBound(groups) & " | group= " & groups(UBound(groups))
    End If

Next i

End Function

这是调试输出:

X1: 1
X1: 4
X1: 4
X1: 5
X2: size/index = 1 | group= 1
X2: size/index = 2 | group= 4
X2: size/index = 3 | group= 5
X3: index = 1 | group= 0
X3: index = 2 | group= 0
X3: index = 3 | group= 5

0 个答案:

没有答案