Dynamic Array problem

  • Thread starter Jean-Jerome Doucet via OfficeKB.com
  • Start date
J

Jean-Jerome Doucet via OfficeKB.com

Hi,

I want to put data in a dynamic Array with a looping. If I use a variable to
change the position in my vurtual dynamic array,it gives me an error saying I
am out of range. The solution I've found and tried in other posts is to redim
the array, but by doing so I loose all the other datas from the previous
positions (other than the actual position redimed). SO is there a way make my
dynamic array works?

Thanks!

JJD

Sub Age_moyenne_ponderee()

'Je déclare es tableaux et mes variables que j'aurai besoin pour
incrémenter.
Dim tabdynamique() As Double
Dim I As Long
Dim J As Variant
J = 0
Dim Z As Integer

' Age looping. Le principe est que la boucle effectue le calcul sur la ligne
la ligne si _
elle voit qu'elle n'est pas vide et change de ligne une fois la
suppression _
de la ligne effectuée.

For I = 12 To 65536

If Worksheets("Formulaire").Range("A" & I).Value <> "" Then

J = J + 1
ReDim tabdynamique(J)
tabdynamique(J) = (((Worksheets("Formulaire").Range("E" & I).Value)
/ (Worksheets("Formulaire").Range("R5").Value)) * (Worksheets("Formulaire").
Range("H" & I).Value))

Sheets("Formulaire").Range("AF" & I).Value = tabdynamique(J)
Else
Exit For
End If
Next I

End Sub
 
M

Michael Bauer

Salut Jean,

calling Redim Preserve tabdynamique(J) preserves the previous array
elements.

Anyway, in your sample I´d go another way. It´s very expensive (in time)
to call Redim again and again. Instead you could dim the array once at
the beginning for the maximum size, count the needed space in J as you
do already, and trim the array once in the end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top