Thanks for your help!!
My question is as follow. I have a section of code that intends to get data from a table and store it into a user defined type. Since the table might change size, the idea is to dynamically give the size to the elements of the type. The type has two one dimensional arrays and one two dimensional array. I am having trouble with the two dimensional array. Can Excel VBA support this feature?
Private Type testing_thermo_data
'one dimesional arrays
temperature() As Double
pressure() As Double
'two dimensional array
composition() As Double
End Type
sub read_from_sheet_to_type()
Dim data as testing_thermo_data
Dim a,b,i as integer
'a and b are determined with some other function (it works), so to simplify I will set them to a number
a=20
b=10
'Now I will reDim the elements of the UDT to the proper size
'One dimension array with a=10 elements
ReDim data.temperature (a) as double
'One dimension array with a=10 elements
ReDim data.pressure (a) as double
'Two dimensional array. Matriz of a=10 by b=20
ReDim data.composition (1 To a, 1 To b) as double
For i = 0 To (a- 1)
data.temperature(i) = Application.ActiveSheet.Cells(i + 3, 1).Value
data.pressure(i) = Application.ActiveSheet.Cells(i + 3, 2).Value
For j = 0 To (b-1)
'This is the line where my code crashes
data.composition(i, j) = Application.ActiveSheet.Cells(i, j + 3).Value
Next j
Next i
end sub
$Once I run the code I get the following error as shown in the picture. Run-time error '1004': Application-defined or object-defined error
I figured it out. The problem was that the cell where I was reading the data from were formatted as general rather than number. That is why it was not reading the data from excel worksheet. I tested this by assigning a constant value "500" to the element data.composition(i,j) and it worked. Here is what I did to test that hypothesis.
Once the problem was identified I fixed the code. Here is how the final code looks. It works perfectly. Thanks for your help!