尝试访问Excel VBA用户定义函数返回的数组时无法分配给数组错误

时间:2019-03-14 12:03:52

标签: arrays excel user-defined-functions

我非常感谢某人对此的帮助,这使我发疯!

我只是尝试使用用户提供的时间段作为参考点来生成12个月度周期的数组。正如我已经测试过的那样(在UDF本身内),UDF正常工作,它返回的数组包含预期值。我想知道我在做错什么吗?

预先感谢您的帮助。

Sub TestPeriods()
Dim CurrentPeriod As Long
Dim arrPeriods(1 To 12) As Variant

CurrentPeriod = 201901
arrPeriods = GeneratePeriods(CurrentPeriod) 'Error on this line

End Sub

Function GeneratePeriods(CurrentPeriod As Long) As Variant

Dim CurrentPeriodMonth As Integer, PeriodSet(1 To 12) As Variant
Dim CurrentYear As Integer, PriorYear As Integer
CurrentPeriodMonth = CurrentPeriod Mod 100
CurrentYear = (CurrentPeriod - (CurrentPeriod Mod 100)) / 100
PriorYear = CurrentYear - 1

If CurrentPeriodMonth < 12 Then
    For i = 1 To CurrentPeriodMonth
        PeriodSet(i) = CLng(CurrentYear) * 100 + i
    Next i

    For i = CurrentPeriodMonth + 1 To 12
        PeriodSet(i) = CLng(PriorYear) * 100 + i
    Next i

Else
    For i = CurrentPeriodMonth To 12
        PeriodSet(i) = CLng(CurrentYear) * 100 + i
    Next i
End If

GeneratePeriods = PeriodSet

End Function

0 个答案:

没有答案