3

I am trying to slice an array into several different arrays (into i arrays), Like this:

Dim a As Double
Dim i,u,e,Number As Integer
Dim BigArray As Double
Redim BigArray(a,3)


For i=0 to Number-1
      Dim Arrayi() as Double
      redim Arrayi(0 to Round((i+1)*a/Number,0)-Round(i*a/Number,0)-1,3)

      For e=0 to 3
      For u=0 to UBound(Arrayi())
          Arrayi(u,e)=BigArray(u+Round(i*a/Number,0),e)
      Next u
      Next e
Next i

Now this works if I manually declare my arrays "Arrayi" and do not loop for i, but I would like to know if there was a way to be able to declare "Array0", "Array1" etc automatically.

The numbers of Array I will need will change from project to project, and having to manually declare them and assign them values is a huge time waste.

Thanks for your help!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • What about using [a jagged array](https://stackoverflow.com/questions/9435608/how-do-i-set-up-a-jagged-array-in-vba)? – Comintern Aug 23 '18 at 21:32
  • Thanks for the advice @Comintern, I will try to see if a jagged array could work for me (although I think having separate named arrays would be easier on me as I intend to move some elements from array to array later on). – Antoine Carpentier Aug 23 '18 at 21:56
  • If having them named is important, take a look at the answers that store arrays in a `Collection` - you can key those with names that you generate at run-time. – Comintern Aug 23 '18 at 22:02
  • 1
    FYI, when you "dim" multiple variables on a single line like "Dim i,u,e,Number as Integer" only the last variable will be set as Integer, the others will be Variants. – twegner Aug 23 '18 at 22:28
  • Whenever you're thinking about creating variables "on the fly" you're typically going down the wrong path: there's no mechanism for doing this in VBA, and even if there was, there's no mechanism by which you could later refer to them... `Array0` and `Array0` for example are better as (eg) `arraySet(0)` and `arraySet(1)` – Tim Williams Aug 23 '18 at 23:00
  • @twegner I didn't know that. It's one of the issue of being self-taught, you get bag habits. Thanks for correcting that! – Antoine Carpentier Aug 24 '18 at 01:46
  • 1
    @TimWilliams so how would you handle a situation such as mine, when you don't know how many arrays you would need? Creating 50 arrays and then using a succession of if statements to fill them one by one? – Antoine Carpentier Aug 24 '18 at 01:48
  • 1
    No - I'd do what @Comintern suggested and use an array of arrays. Or a collection of arrays. – Tim Williams Aug 24 '18 at 02:23
  • 1
    Agree with Tim and Comintern. In your case though, most applicable would be a collection of array where you have a dummy array redimmed each time to get the slices and then dumped in a collection which would have a key. – L42 Aug 24 '18 at 03:34
  • The array of array worked like a charm, thanks for your input guys! – Antoine Carpentier Aug 26 '18 at 00:05

0 Answers0