1
votes

I am trying this snippet of code and cannot get rid of the error "ByRef argument type mismatch".

I have declared i and j as integers and I have put ByRef in the DisplayTreeHelp function to keep the value that I get for i and j once I finish to execute DisplayTreeHelp function. (otherwise the value goes back to the value it had before entering DisplayTreeHelp). j however is keeping the value either if I do not specify the ByRef in the DisplayTreeHelp function.

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

Recursion function

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

Anyone has an idea on this? Thanks in advance!

2

2 Answers

8
votes

i and j aren't declared as Integer, they are implicitly Variant. On these declaration lines...

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

...only the last variable in the list is strongly typed. You need to specify a type for each one:

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

"I have declared i and j as integers"

You did not do that - i and j are Variants, not Integers. In VBA you must type every variable individually - you can't just type the last one in a comma-separated list of declarations.

Dim i, i_max As Integer

should be

Dim i As Integer, i_max As Integer