Lucas Raphael Pianegonda
Lucas Raphael Pianegonda

Reputation: 1181

How to pass variant to double array VBA?

My idea was to create a function to pass double arrays like this:

Function pass(a() As Double, b() as double) As Boolean
Dim i As Integer, j As Integer
ReDim a(0 To UBound(b, 1), 0 To UBound(b, 2))
    For i = 0 To UBound(a, 1)
        For j = 0 To UBound(a, 2)
            a(i, j) = b(i, j)
        Next
    Next
End Function

such that one could use:

pass(a,b)

and then a=b.

a=b

doesn't work.. (oh god I miss matlab..)

OK, back to the problem:

This works fine for two double arrays but if I get a variant, and as I use this I can get a variant, this doesn't work. So is there a solution for passing variant values to double arrays? The data is structured like a double array but has not the type double().

Upvotes: 1

Views: 3210

Answers (1)

Tolure
Tolure

Reputation: 879

To solve this I would remove the types of the parameters.

Sub test()

Dim VarArry(0 To 4, 0 To 4) As Variant
Dim DblArry(0 To 4, 0 To 4) As Double
Dim VarAssing() As Variant
Dim DblAssing() As Double
Dim c As Boolean

VarArry(0, 1) = 34
DblArry(0, 1) = 34

c = pass(VarAssing, VarArry)
Debug.Print (TypeName(VarAssing))
Debug.Print (VarAssing(0, 1))

c = pass(VarAssing, DblArry)
Debug.Print (TypeName(VarAssing))
Debug.Print (VarAssing(0, 1))

c = pass(DblAssing, VarArry)
Debug.Print (TypeName(DblAssing))
Debug.Print (VarAssing(0, 1))

c = pass(DblAssing, DblArry)
Debug.Print (TypeName(DblAssing))
Debug.Print (VarAssing(0, 1))


End Sub

Function pass(a, b) As Boolean
Dim i As Integer, j As Integer
ReDim a(0 To UBound(b, 1), 0 To UBound(b, 2))
    For i = 0 To UBound(a, 1)
        For j = 0 To UBound(a, 2)
            a(i, j) = b(i, j)
        Next
    Next
End Function

The output was as follows

Variant()
 34 
Variant()
 34 
Double()
 34 
Double()
 34 

Upvotes: 2

Related Questions