【问题标题】:ByRef argument type mismatch for integers整数的 ByRef 参数类型不匹配
【发布时间】:2018-08-17 15:14:09
【问题描述】:

我正在尝试这个 sn-p 代码,但无法摆脱错误“ByRef 参数类型不匹配”。

我已将 i 和 j 声明为整数,并将 ByRef 放入 DisplayTreeHelp 函数中,以便在完成执行 DisplayTreeHelp 函数后保留 i 和 j 获得的值。 (否则该值会回到进入 DisplayTreeHelp 之前的值)。但是,如果我没有在 DisplayTreeHelp 函数中指定 ByRef,j 会保留该值。

Public Sub DisplayTree()
    'Given country
    Dim country As String
    country = Sheets("Tree").Range("Country").Value2

    'Start cell
    Dim start As Range
    Set start = Range("A2")

    Dim dic1 As Dictionary
    Dim key1 As Variant
    Dim i, i_max As Integer
    Dim j, j_max As Integer

    Set dic1 = CreateTree
    'Column start
    j = start.Column
    j_max = j
    'Row start
    i = start.Row
    i_max = i

    Dim tempArrSize As Long
    tempArrSize = 1

    Dim tempArr1() As Variant
    ReDim tempArr1(1 To 1)

    'Iterate dictionary to get nodes with parent node 0
    For Each key1 In dic1.Keys()
        If dic1(key1).country = country Then
            If dic1(key1).parentNodeId = 0 Then
                'make array 1 bigger
                tempArrSize = tempArrSize + 1
                ReDim Preserve tempArr1(1 To tempArrSize + 1)
                'store it in an array
                tempArr1(dic1(key1).OrderId) = key1
            End If
        End If
    Next

    If Not IsEmpty(tempArr1) Then
        Dim key2 As Variant
        For Each key2 In tempArr1
            If Not IsEmpty(key2) Then
                i = i + 1
                If i > i_max Then
                    i_max = i
                End If

                Sheets("Tree").Cells(i, j).Value = dic1(key2).NodeName

                Dim dic2 As Dictionary
                Set dic2 = dic1(key2).ChildNodes

                Call DisplayTreeHelp(dic2, i, j, i_max, j_max)
            End If
        Next
    End If

    Call Format_tree(start, i_max, j_max)
End Sub

递归函数

Private Function DisplayTreeHelp(dic2 As Dictionary, ByRef i As Integer, ByRef j As Integer, ByRef i_max As Integer, ByRef j_max As Integer) As Variant

    If dic2.Count = 0 Then
        'Do nothing
    Else
        Dim key3 As Variant
        Dim tempArr2() As Variant
        ReDim tempArr2(1 To dic2.Count + 1)

        j = j + 1
        If j > j_max Then
            j_max = j
        End If

        'Create array with the proper order within the bucket
        For Each key3 In dic2.Keys()
            'Add all keys to array in the index of the order id
            tempArr2(dic2(key3).OrderId) = key3
        Next

        If Not IsEmpty(tempArr2) Then
            Dim key4 As Variant
            For Each key4 In tempArr2
                If Not IsEmpty(key4) Then
                    i = i + 1
                    If i > i_max Then
                        i_max = i
                    End If
                    Sheets("Tree").Cells(i, j).Select
                    Selection.Value = dic2(key4).NodeName

                    Call DisplayTreeHelp(dic2(key4).ChildNodes, i, j, i_max, j_max)
                End If
            Next key4
            j = j - 1
        End If
    End If
End Function

有人对此有想法吗?提前致谢!

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    ij 未声明为 Integer,它们隐含为 Variant。在这些声明行...

    Dim i, i_max As Integer
    Dim j, j_max As Integer
    

    ...只有列表中的 last 变量是强类型的。您需要为每个指定一个类型:

    Dim i As Integer, i_max As Integer
    Dim j As Integer, j_max As Integer
    

    【讨论】:

    • @MTT 注意Rubberduck 会警告你这些(免责声明:共产国际和我都是这个 OSS 项目的贡献者) - 另请注意,Integer 是一个 16 位有符号整数类型,因此它的最大值远小于您在工作表中可以拥有的行数;首选Long,32 位有符号整数类型,以避免整数溢出错误。
    【解决方案2】:

    “我已将 i 和 j 声明为整数”

    您没有这样做 - i 和 j 是变体,而不是整数。 在 VBA 中,您必须单独键入每个变量 - 您不能只在以逗号分隔的声明列表中键入最后一个变量。

    Dim i, i_max As Integer
    

    应该是

    Dim i As Integer, i_max As Integer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      相关资源
      最近更新 更多