vba - 从函数返回数组时类型不匹配

标签 vba excel

我正在尝试创建一个函数,用于在 subs 中使用,该函数创建一个月中实际天数的数组。我无法直接使用立即窗口或使用子窗口来运行该函数。该函数的代码如下(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

使用立即窗口和 GenerDager(2, 2017) 运行该函数会产生以下错误消息:编译错误:预期:=

使用立即窗口与 ?GenererDager(2, 2017) 运行该函数会产生以下错误消息:编译器错误:类型不匹配 与 daysArr 的最后一次使用已选择

我用来调用该函数的测试子如下所示:

Sub HentDager()

    Dim daysArray() As Integer

    daysArray = GenererDager(2, 2017)

    Debug.Print daysArray(4)

End Sub

在立即窗口中使用 HentDager() 调用此子程序会产生以下错误:编译错误:预期:=

我已经被这个问题困扰了一段时间,并且我已经多次重写代码来找出问题,但到目前为止我还无法解决它。我可能产生的错误比我在此过程中修复的错误还要多,因为我突然意识到我不知道我现在真正在做什么:-)

最佳答案

您必须将对象和函数声明为 Variant 才能传递数组。

此外,您的函数需要进行一些更改:

  • actualDays =actualDays - 1因为您返回的第一个值不是日期!
  • ReDim daysArr(1 ToactualDays) 以避免数组的第一个索引为空值,
    这本来是 daysArr(0)

工作功能(已测试):

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

测试:

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

关于vba - 从函数返回数组时类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42203557/

相关文章:

excel - VBA在工作表的给定范围内保护和取消保护

vba - Access VBA 从月数中获取季度

excel - 为什么智能表格中没有设置正确的字体颜色?

html - 在 Excel 中居中图像

html - 使用 VBA 和 Selenium 驱动网站

loops - 为什么这个 IF 语句不需要 End IF

excel - 停止 VBA Evaluate 调用目标函数两次

Excel将我的数字变成 float

c# - Excel 和 C#.NET

excel - 为数组分配范围时出现 VBA 内存问题