PDA

View Full Version : Dimension x number of arrays based on user input



Saladsamurai
01-27-2010, 11:51 AM
I am writing a code that needs an array for each Element of the problem to be solved. The number of elements will be put by the user.

I need VBA to define a new array for each element from 1 to the number of elements. I am trying something like this:



Sub Assemble()

Dim i As Integer, j As Integer
Dim nElements As Integer, nNodes As Integer

For i = 1 To 3
Dim Ki(5) As Double
Next i

End Sub




I would like this to define 3 arrays named K1, K2, K3 each of which has an Ubound of 5.

Excel is just defining 1 array called Ki 3 times when I use the above.

How can I do this right?

Thanks!

Bob Phillips
01-27-2010, 12:09 PM
Sub Assemble()
Dim K, tmp
Dim i As Integer, j As Integer
Dim nElements As Integer, nNodes As Integer

ReDim K(1 To 3)
ReDim tmp(1 To 5)
For i = 1 To 3
K(i) = tmp
Next i

End Sub

Saladsamurai
01-27-2010, 12:26 PM
Sub Assemble()
Dim K, tmp
Dim i As Integer, j As Integer
Dim nElements As Integer, nNodes As Integer

ReDim K(1 To 3)
ReDim tmp(1 To 5)
For i = 1 To 3
K(i) = tmp
Next i

End Sub


So what exactly does this do xld? Is it an array full of arrays? Nasty!

Bob Phillips
01-27-2010, 01:00 PM
Yes it is, and it is elegant not nasty.

Saladsamurai
01-27-2010, 07:24 PM
Yes it is, and it is elegant not nasty.

Yes of course. I meant nasty in the most elegant way. Like when the kids say "damn that Benz was nasty."

Bob Phillips
01-28-2010, 01:10 AM
I know what you meant :) I was just playing back.

Tommy
01-28-2010, 01:24 PM
Why not a doulbe dimension array?
reDim K(1 to 3,1 to number entered in textbox)?
K(1)(2) is not as easy (IMHO) as K(1,2)

just thinking with my fingers :)

Bob Phillips
01-28-2010, 03:07 PM
Why not a doulbe dimension array?
reDim K(1 to 3,1 to number entered in textbox)?

You can't have different sized arrays then, everything is the same.


K(1)(2) is not as easy (IMHO) as K(1,2)

just thinking with my fingers :)

You are obviously not a JavaScript man :)

Tommy
01-28-2010, 03:21 PM
I had to work with ex K()()(,)() somehow or other they were filling it with reg expressions and I just got a bad taste in my mouth I guess.

JavaScript :bug: I haven't taken the time to figure out VBScript! :devil2: