简体   繁体   中英

ReDim multidimentional arrays in User Defined Type in EXCEL vba

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 c ell 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.

for i=1 To a
For j = 1 To b
'This is the line where my code crashes
data.composition(i, j) = 500
Next j
Next i

Once the problem was identified I fixed the code. Here is how the final code looks. It works perfectly. Thanks for your help!

Option Base 1
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),
'to simplif 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 = 1 To (a)
data.temperature(i) = Application.ActiveSheet.Cells(i + 3, 1).Value
data.pressure(i) = Application.ActiveSheet.Cells(i + 3, 2).Value
   For j = 1 To (b)
   '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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM