Array of User define data.

  • Thread starter Thread starter =?gb2312?B?Q2FjdHVzIFvPycjLx/Jd?=
  • Start date Start date
?

=?gb2312?B?Q2FjdHVzIFvPycjLx/Jd?=

I want to use a typed data of array.

Type myType
X As Long
Y As Long
End Type

Dim myMatrix(20)(20) As myType

But above code is wrong.
How to that right VBA coding?

Another question.
Can I use a Variant array such as

dim myVar as Long = 20
Dim myMatrix(myVar) As myType


Thanks...
 
I think this is what you mean

Dim myarray() As myType

ReDim myarray(1 To 20)
myarray(1).X = 1
myarray(1).Y = 3

Const cLen As Long = 20
Dim myArray2(cLen) As myType

myArray2(20).X = 1
myarray(20).Y = 2
 
Thank Bob

I read VBA help, to define a 2D array use

Dim myArray(20,20) as myType

Now I get next block.

How send the 2D Array as Argument?

Sub mySub(theArray() As myType)

End Sub
 
Type myType
X As Long
Y As Long
End Type


Sub MySub1()
Dim myArray(20, 20) As myType
For i = LBound(myArray, 1) To UBound(myArray, 1)
For j = LBound(myArray, 2) To UBound(myArray, 2)
myArray(i, j).X = Int(Rnd() * 10000 + 1)
myArray(i, j).Y = Int(Rnd() * 10000 + 1)
Next
Next
mySub myArray
End Sub


Sub mySub(theArray() As myType)
i = Int(Rnd() * 20 + 1)
j = Int(Rnd() * 20 + 1)
MsgBox "MyArray(" & i & "," & _
j & ").X=" & theArray(i, j).X & _
vbNewLine & _
"MyArray(" & i & "," & _
j & ").Y=" & theArray(i, j).Y
End Sub

as an example.
 
Hi Tom

I want the argument is a part of array.
but VBA said that is wrong language grammar.


Sub mySub(theArray() As myType)
For y = 0 to 20
theArray(y).X = 1
theArray(y).Y = 2
Next y
End Sub


Sub Main()
Dim myArray(20, 20) As myType
For x = 0 to 20
mySub myArray(x)
'I think myArray(x) should a array "myArray(20) As myType"
Next x
End Sub
 
You cannot pass 1 dimension of the array to the sub AFAIK, but you can pass
1 element

Type myType
x As Long
y As Long
End Type

Sub mySub(theArray As myType)
With theArray
.x = .x * 2
.y = .y * 2
End With
End Sub


Sub Main()
Dim myArray(20, 20) As myType, x
For x = 0 To 20
myArray(x, 1).x = 100 + x
myArray(x, 1).y = 200 + x
MsgBox "x=" & myArray(x, 1).x & ", Y=" & myArray(x, 1).y
mySub myArray(x, 1)
MsgBox "x=" & myArray(x, 1).x & ", Y=" & myArray(x, 1).y
Next x
End Sub
 
Back
Top