ByRef Argument type mismatch on just changing the variable position in declaration

118 views Asked by At

I have defined a function in VBA as follows:

Sub TestFunction()    
Dim ArrayLength, IDvariable, IDComparisonResult, PreArrayLength As Integer
ReDim NodesArray(0)
PreArrayLength = 0
IDvariable = 0
.
.
Sort PreArrayLength
End Sub

whereas the function called is as follows:

Sub Sort (PreArrayLength As Integer)
.
.
.
end sub

Above function runs nicely but if I change declaration in TestFunction() as

Dim ArrayLength, IDvariable, PreArrayLength, IDComparisonResult As Integer

my code gives me an error "ByRef Argument type mismatch" indicating the line

Sort PreArrayLength

Can anyone point the mistake I am making in declaration or understanding the error?

1

There are 1 answers

0
R3uK On BEST ANSWER

It is as simple as this :

Dim ArrayLength As Integer, _
    IDvariable As Integer, _
    PreArrayLength As Integer, _
    IDComparisonResult As Integer

Because when you use Dim, you have to specify for each variable what type is it, the previous code was declaring the first three as Variant and only the last one as Integer!

Reference: How to declare variables in VBA