2-Dimensional and different-data-type array decleration

eros

Board Regular
Joined
May 6, 2011
Messages
90
Good day,

Is there a way to declare a 2-dimensional array, starting from 1 upto 1000 for each dimension and most importantly consisting of different data type?

What I know is:
Code:
Dim myArray(1 to 1000) As Integer
This provides first array index 1 as opposed to 0 but it is one dimensional
Code:
Dim myArray(1 to 1000, 1 to 1000) As integer
This is a 2-D array but all dimensions of type integer, not of different type.
I want to declare an array one dimension of which is sth like the Type below:

Code:
Public Type myType
  x As Integer
  y As Integer
  str As String
  lng As Long
End Type

And based on what I know above, I tried:
Code:
Dim myArray(1 to 1000 As Integer, 1 to 1000 As myType)
but this fails...

Why on earth I would want this: Because, I want to run a loop to assign values to textBox objects which form a UserForm. By array-like indexing it is easy to reach out controls within loop. If I used Type decleration I would have to manually type each different bit of the myType type.

Many thanks in advance
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Code:
Dim myArray(1 to 1000) As myType
 
Upvote 0
Thanks but it is one-dimensional. I need a 2-D array with 1000 items of integer and 1000 items as myType.
 
Upvote 0
You can't have an array like that, other than declaring a 1000 x 1000 variant array, and then setting all but the first column to myType, and you would lose IntelliSense.

More broadly, what are you trying to do?
 
Upvote 0
Hi

Not sure I understand exactly, but you can define a specific user type for that, using the type you've already defined:

Code:
Option Explicit
 
Type myType
    x As Long
    y As Long
    str As String
    lng As Long
End Type
 
Type myArrayType
    i As Long
    Typ As myType
End Type
 
Dim myArray(1 To 1000) As myArrayType
 
Sub x()

myArray(400).i = 12345
myArray(400).Typ.x = 67890
myArray(400).Typ.str = "Hi"
 
MsgBox _
    "i: " & myArray(400).i & _
    ", x: " & myArray(400).Typ.x & _
     ", str: " & myArray(400).Typ.str
End Sub
 
Upvote 0
That's still a 1D array, is it not, pgc?
 
Upvote 0
That's still a 1D array, is it not, pgc?

Hi shg

You are right, it's a 1D array, but since it's a 1D array of a user type with 2 elements (at least in the first level) it's like a 1D array of 2D elements, in which we can define the types as we want.

It's not exactly what the OP wants, but it's a solution that the vba syntax allows and I hope this will come close to what the OP needs.
 
Upvote 0
Thank you guys for your prompt return. Higly appreciated.

To me, putting i into myArrayType instead of myType does not bring any advantages.

What I am trying to do, in fact, is that I want to be as much flexible as Type decleration allows me to use different data types as well as not to have to refrain from the comfort of numerical indexing as arrays offer. All at once in a 2-D array to use it, say, within loops. However, from the references you suggest above I understand what I want cannot be achieved. Or, can it? Let me try my chance:
Can I write this references, particularly of myType using array indices?
myArray(400).Typ.x = 67890
myArray(400).Typ.str = "Hi"
instead of above, something like:
Code:
myArray(400).Typ(0) = 67890
myArray(400).Typ(1) = "Hi"
or even better
Code:
myArray(400).(0) = 67890
myArray(400).(1) = "Hi"
with 0, 1 referring to first, second and so forth items in myType.
Looping with this structure would have been very comfortable and this is what I am trying to achieve.

I am not sure what it means, but IntelliSense sounds to be something that we don't want to lose by using variant type as proposed. Is it important?

Again, many thanks for your kind and prompt return.
 
Upvote 0
Maybe a structure like this will do what you want (note I provided an example for one element)....
Code:
Type myType
  x As Integer
  y As Integer
  str As String
  lng As Long
End Type
 
Type TwoDArray
  L() As Long
  T() As myType
End Type
 
Sub TEST()
  '  Create the 2D array holder
  Dim MyArr As TwoDArray
 
  '  Create the elements of the "2D Array"
  ReDim MyArr.L(1 To 1000)
  ReDim MyArr.T(1 To 1000)
 
  '  Load up the array (here is an example of only one 2D element being filled)
  MyArr.L(999) = 123
  MyArr.T(123).x = 11
  MyArr.T(123).y = 22
  MyArr.T(123).str = "Some text"
  MyArr.T(123).lng = 999
 
  '  Let's see that the assignment were made
  Debug.Print MyArr.L(999), MyArr.T(123).x, MyArr.T(123).y, MyArr.T(123).str, MyArr.T(123).lng
End Sub
 
Upvote 0
Hi Rick,

Thank you for your kind return.

Would you agree with me if I say that I cannot relate the array indexes in the structure you suggest. I mean I cannot identify an individual element for, say, the 2nd row and 5th column given that a 2D array is in fact a table. Your structure does not seem to allow identifying individual elements of 2D arrays as in the following code

Code:
myArray(2,5)=any variable

Therefore, I have a strong feeling that unfortunately your sample code does not show array characteristics.

Am I right to think so?

Many thanks for your assistance
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top