从函数返回数组时从types不匹配

我正在尝试创build一个函数,用于在subs中创build一个月的实际date数组。 我无法直接使用“立即”窗口或使用子窗口来运行该function。 该函数的代码如下( Debug.Print实例被删除,因为他们甚至从来没有触发):

 Function GenererDager(ByVal month As Integer, ByVal year As Integer) As Integer Dim dateFormat As String, daysArr() As Integer, actualDays As Integer actualDays = 1 ' Find the number of actual days in given month For dayCheck = 1 To 31 dateFormat = month & "/" & dayCheck & "/" & year ' mm/dd/yyyy format If IsDate(dateFormat) Then actualDays = actualDays + 1 Else Exit For End If Next dayCheck ' Redimension the array with the actual number of days in the month ReDim daysArr(actualDays) ' Populate the array with the correct number of days For daysToArray = 1 To actualDays daysArr(daysToArray) = daysToArray Next daysToArray GenererDager = daysArr End Function 

GenererDager(2, 2017)使用立即窗口运行函数会产生以下错误消息: Compile error: Expected: =

使用带有?GenererDager(2, 2017)的立即窗口运行函数会产生以下错误消息: Compiler error: Type mismatch与所选daysArr的最后一次使用Compiler error: Type mismatch

我用来调用函数的test-sub看起来像这样:

 Sub HentDager() Dim daysArray() As Integer daysArray = GenererDager(2, 2017) Debug.Print daysArray(4) End Sub 

使用HentDager()在立即窗口中调用此子产生以下错误: Compile error: Expected: =

我已经在这个问题上困扰了一段时间了,为了确定问题,我已经多次重写代码,但到目前为止,我一直无法解决这个问题。 我可能会产生比我在这个过程中修正的错误更多的错误,因为我已经意识到我不知道我现在正在做什么:-)

你必须声明你的对象和函数作为Variant来传递数组。

此外,你的function需要一些改变:

  • actualDays = actualDays - 1因为您返回了不是date的第一个值!
  • ReDim daysArr(1 To actualDays)避免在数组的第一个索引上有一个空值,
    这将是daysArr(0)

工作function(testing):

 Function GenererDager(ByVal month As Integer, ByVal year As Integer) As Variant Dim dateFormat As String, daysArr() As Variant, actualDays As Integer, dayCheck As Integer, daysToArray As Integer actualDays = 1 ' Find the number of actual days in given month For dayCheck = 1 To 31 dateFormat = month & "/" & dayCheck & "/" & year ' mm/dd/yyyy format If IsDate(dateFormat) Then actualDays = actualDays + 1 Else Exit For End If Next dayCheck actualDays = actualDays - 1 ' Redimension the array with the actual number of days in the month ReDim daysArr(1 To actualDays) ' Populate the array with the correct number of days For daysToArray = 1 To actualDays daysArr(daysToArray) = daysToArray Next daysToArray GenererDager = daysArr End Function 

testing:

 Sub HentDager() Dim daysArray() As Variant daysArray = GenererDager(2, 2017) 'Display last day of the month in the immediate window Debug.Print daysArray(UBound(daysArray)) End Sub 

经过一些试验和错误,这里是一个使用Integer的例子:

 Sub TestIntSub() Dim TestInt() As Integer TestInt = FctIntArr Debug.Print TestInt(1) & "|" & TestInt(2) End Sub Public Function FctIntArr() As Integer() Dim IntArr() As Integer ReDim IntArr(1 To 2) IntArr(1) = 545 IntArr(2) = 232 FctIntArr = IntArr End Function 

您需要正确定义函数的返回types –

 Function GenererDager(ByVal month As Integer, ByVal year As Integer) As Integer 

将其更改为

 Function GenererDager(ByVal month As Integer, ByVal year As Integer) As Variant 

很less有事情似乎是错的

  1. 你的函数返回值实际上是一个整数而不是整数()

  2. 而不是调暗daysarray作为数组,只是将其调暗为一个变体并赋值