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
 
Rick,

As an alternative I have just composed the following code using Array of Arrays approach to define a 1000x5-element 2D-array holding different data type in the 2nd index.

Code:
'Alternative: Jagged Arrays or Array of Arrays
Type myType   ' for the 2nd index of my 2D array holding different data types
  x As Integer
  y As Integer
  str As String
  lng As Long
  dbl As Double
End Type
 
Sub Test()
  Dim ArraySub() As myType '2nd index of my 2D array
  Dim ArrayMain(1 To 1000) As Variant  '1st index of my 2D array
  For i = 1 To 1000
    ReDim ArraySub(1 To 5) As myType
    ArrayMain(i) = ArraySub
  Next
 'Test if I managed to create a 2D array of different data types
  ArrayMain(1)(1).x = 10
  ArrayMain(1)(3).y = 20
  ArrayMain(1)(5).str = "hello"
  ArrayMain(1000)(2).lng = 12345678
  ArrayMain(1000)(5).dbl = 123456.123456
  MsgBox (CStr(ArrayMain(1)(1).x) & " " & CStr(ArrayMain(1)(3).y) & " " & ArrayMain(1)(5).str & " " & CStr(ArrayMain(1000)(2).lng) & " " & CStr(ArrayMain(1000)(5).dbl))
End Sub

However this code generates a Compile Error:
Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound function.

This clearly means nothing to me. What would be wrong here?
 
Last edited:
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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