IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Problème avec le "ReDim" d'un tableau


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Janvier 2012
    Messages
    233
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2012
    Messages : 233
    Par défaut Problème avec le "ReDim" d'un tableau
    Bonjour,

    Je cherche à redimmensionner un array dans mon code. L'array s'appelle tabl.
    Dans le bout de code suivant, lorsque j'omet le
    Preserve
    , je perds mes données déjà stockées dans le tableau.
    Mais lorsque je rajoute le
    Preserve
    après
    Redim
    , ca compile plus...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
        'défini le tableau qui stocke les titres encore portés
        maxTabl = 1
        ReDim tabl(1 To maxTabl, 1 To 8) As Variant
        'calcule le max de ligne dans la feuille Bonds du classeur source
        maxLigneBond = Workbooks(nomFichierImport).Worksheets(wsBond).Range("A" & Rows.Count).End(xlUp).Row
        For i = 5 To maxLigneBond
            For j = 1 To maxTabl
     
                If tabl(j, 1) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("B" & i) Then
                    tabl(j, 8) = tabl(j, 8) + Workbooks(nomFichierImport).Worksheets(wsBond).Range("H" & i)
                    GoTo suite
                Else
     
                    If j = maxTabl Then
     
                        maxTabl = maxTabl + 1
                        ReDim Preserve tabl(1 To maxTabl, 1 To 8)
                        tabl(maxTabl, 1) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("B" & i)
                        tabl(maxTabl, 2) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("C" & i)
                        tabl(maxTabl, 3) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("D" & i)
                        tabl(maxTabl, 4) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("E" & i)
                        tabl(maxTabl, 5) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("Z" & i)
                        tabl(maxTabl, 6) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("AA" & i)
                        tabl(maxTabl, 7) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("AB" & i)
                        tabl(maxTabl, 8) = Workbooks(nomFichierImport).Worksheets(wsBond).Range("H" & i)
                    End If
     
                End If
     
            Next
    suite:
        Next
    Quelqu'un voit le pb?

    Merci d'avance!

  2. #2
    Expert éminent Avatar de mercatog
    Homme Profil pro
    Inscrit en
    Juillet 2008
    Messages
    9 435
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations forums :
    Inscription : Juillet 2008
    Messages : 9 435
    Par défaut
    On ne peux redimensionner que la 2ème dimension d'une variable tableau.

    EDIT

    Pour pallier à cela, on utilise un tableau inversé qu'on transpose à la fin. Je tiens aussi à utiliser un tableau temporaire, au lieu des va et vient entre code et feuille Excel

    Exemple à adapter selon le besoin
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    Sub Test()
    Dim Tabl, Tmp
    Dim MaxTabl As Long, MaxLigneBond As Long, i As Long, j As Long
     
    Application.ScreenUpdating = False
    With Workbooks(nomFichierImport).Worksheets(wsBond)
        MaxLigneBond = .Range("A" & .Rows.Count).End(xlUp).Row
        Tmp = .Range("A5:AB" & MaxLigneBond)
    End With
     
    MaxTabl = 1
    ReDim Tabl(1 To 8, 1 To 1)
    For i = 1 To UBound(Tmp)
        For j = 1 To MaxTabl
            If Tabl(1, j) = Tmp(i, 2) Then
                Tabl(8, j) = Tabl(8, j) + Val(Tmp(i, 8))
                Exit For
            Else
                If j = MaxTabl Then
                    MaxTabl = MaxTabl + 1
                    ReDim Preserve Tabl(1 To 8, 1 To MaxTabl)
                    Tabl(1, MaxTabl) = Tmp(i, 2)
                    Tabl(2, MaxTabl) = Tmp(i, 3)
                    Tabl(3, MaxTabl) = Tmp(i, 4)
                    Tabl(4, MaxTabl) = Tmp(i, 5)
                    Tabl(5, MaxTabl) = Tmp(i, 26)
                    Tabl(6, MaxTabl) = Tmp(i, 27)
                    Tabl(7, MaxTabl) = Tmp(i, 28)
                    Tabl(8, MaxTabl) = Val(Tmp(i, 8))
                End If
            End If
        Next j
    Next i
     
    Feuil2.Range("A1").Resize(MaxTabl, 8) = Application.Transpose(Tabl)
    End Sub

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Janvier 2012
    Messages
    233
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2012
    Messages : 233
    Par défaut
    Merci mercatog!!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Probléme avec la suppression de ligne dans un tableau
    Par Tintou dans le forum VBA Word
    Réponses: 6
    Dernier message: 20/08/2009, 15h06
  2. Problème avec \cline dans la création d'un tableau
    Par Ravens dans le forum Tableaux - Graphiques - Images - Flottants
    Réponses: 5
    Dernier message: 03/12/2007, 20h31
  3. Problème avec les conditions ''sous-ensemble d'un tableau''
    Par djangossoul dans le forum LabVIEW
    Réponses: 2
    Dernier message: 07/06/2007, 12h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo