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 :

Aide pour macro de recherche et copie de lignes entière en utilisant un dictionnaire [XL-2013]


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    employé de bureau
    Inscrit en
    Août 2014
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : employé de bureau
    Secteur : Transports

    Informations forums :
    Inscription : Août 2014
    Messages : 16
    Par défaut Aide pour macro de recherche et copie de lignes entière en utilisant un dictionnaire
    j'ai un code qui me permet de rechercher rapidement la valeur "Obsolète" dans la colonne AC et de récupérer les résultats dans une Listbox mais ce que j'aimerais c'est pouvoir récupérer la ligne entière sachant que le nombre de colonne est variable
    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
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    Sub recherchearchive2()
    Dim dico As Object
    Dim vMsn As Variant
    Dim Last_date As Date
    Dim Last_objet As String
    Dim a(), b(), c()
    Dim i As Long, j As Long, n As Long
    Dim lastrow As Long
    Dim tblo As Variant
    Dim tblo2()
    Dim Temp()
    Dim ligne As Long
    Dim Critere1 As String
    UserForm2.ListBox3.Clear
    Application.ScreenUpdating = False
    DoEvents
    rechercheobsolete
    '**************statut**********************
    Critere1 = "Obsolète"
     
     
    lastrow = ThisWorkbook.Sheets("Cvtheque").Range("A" & Rows.Count).End(xlUp).Row
    Temp = ThisWorkbook.Sheets("Cvtheque").Range("A1:AC" & lastrow).Value
    Set dico = CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(Temp, 1)
            If Temp(i, 29) Like Critere1 Then
                If dico.Exists(Temp(i, 1) & " | " & Temp(i, 14)) = False Then
                    'on ajoute dnas le dico l'objet inexistant
                    dico(CStr(Temp(i, 1)) & "|" & CStr(Temp(i, 14))) = CStr(Temp(i, 14)) & "|" & CStr(Temp(i, 15)) & "|" & CStr(Temp(i, 4)) & "|" & CStr(Temp(i, 2)) & "|" & CStr(i)
                End If
            End If
            Next i
     
    'extraction du resultat
     n = dico.Count
    If n > 0 Then
    ReDim c(1 To n, 1 To 5)
    UserForm2.ALarchive.Visible = False
    ElseIf n = 0 Then
    UserForm2.ALarchive.Visible = True
    Exit Sub
    End If
     
      a = dico.Keys
      b = dico.Items
      For j = 1 To n
     
      'c(i, 1) = a(i - 1)
      c(j, 1) = Split(b(j - 1), "|")(0)
      c(j, 2) = Split(b(j - 1), "|")(1)
      c(j, 3) = Split(b(j - 1), "|")(2)
      c(j, 4) = Split(b(j - 1), "|")(3)
      c(j, 5) = Split(b(j - 1), "|")(4)
     
      Next j
      Call tri(c(), 1, LBound(c, 1), UBound(c, 1))
    UserForm2.ListBox3.ColumnCount = 4
     
      UserForm2.ListBox3.List = c
      UserForm2.Repaint
      UserForm2.LBLwait.Visible = False
    Application.ScreenUpdating = True
    End Sub
    j'ai modifié le début du code pour récupérer uniquement les numéros de lignes qui m'intéressent mais après je vois pas comment alimenter mon tableau c parcourant toutes les lignes dont le numéro est dans le dictionnaire. Voici le début du code modif
    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
    Dim dico As Object
    Dim vMsn As Variant
    Dim Last_date As Date
    Dim Last_objet As String
    Dim a(), b(), c As Variant, k&
    Dim i As Long, j As Long, n As Long
    Dim lastrow As Long
    Dim tblo As Variant
    Dim tblo2()
    Dim Temp()
    Dim dercol As Long
    Dim ligne As Long
    Dim Critere1 As String
    UserForm2.ListBox3.Clear
    Application.ScreenUpdating = False
    DoEvents
    rechercheobsolete
    '**************statut**********************
    Critere1 = "Obsolète"
     
     
    lastrow = ThisWorkbook.Sheets("Cvtheque").Range("A" & Rows.Count).End(xlUp).Row
    dercol = ThisWorkbook.Sheets("Cvtheque").Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    ThisWorkbook.Sheets("Cvtheque").Activate
    Temp = ThisWorkbook.Sheets("Cvtheque").Range(Cells(1, 1), Cells(lastrow, dercol)).Value
    Set dico = CreateObject("Scripting.Dictionary")
    For i = 2 To UBound(Temp, 1)
            If Temp(i, 29) Like Critere1 Then
                If dico.Exists(Temp(i, 1)) = False Then
                    'on ajoute dnas le dico l'objet inexistant
                    dico(CStr(Temp(i, 1)) & "|" & CStr(Temp(i, 14))) = CStr(i)
                End If
            End If
            Next i
    j'avais pensé à faire un truc du genre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
     n = dico.Count
    If n > 0 Then
    ReDim c(1 To n, 1 To 5)
    UserForm2.ALarchive.Visible = False
    ElseIf n = 0 Then
    UserForm2.ALarchive.Visible = True
    Exit Sub
    End If
    b=dico.items
    for j=1 to n
    c=thisworkbook.sheets("Cvtheque").range(cells(b(j-1),1)cells(b(j-1),dercol)).value
    next j
    mais seule la première ligne apparaît, auriez-vous une idée ?

  2. #2
    Membre émérite
    Homme Profil pro
    Programmeur analyste
    Inscrit en
    Février 2009
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : Canada

    Informations professionnelles :
    Activité : Programmeur analyste
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2009
    Messages : 546
    Par défaut
    Bonjour,
    ceci n'est rien que un exemple, à adapter à ton projet.

    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
    Sub test()
     
        Dim arrLstBox(1 To 100, 1 To 50)
        ListBox5.ColumnCount = 50
        Ligne_Listbox = 1
        Set q = Range("AC1:AC" & Cells(Rows.Count, "AC").End(xlUp).Row).Find("Obsolète")
        F = q.Address
     
        Do
     
            arrLstBox(Ligne_Listbox, 1) = q.Value
            arrLstBox(Ligne_Listbox, 2) = q.Offset(0, -1).Value ' colonne AB
            arrLstBox(Ligne_Listbox, 3) = q.Offset(0, -2).Value ' colonne AA
            arrLstBox(Ligne_Listbox, 4) = q.Offset(0, -3).Value ' colonne Z
            arrLstBox(Ligne_Listbox, 5) = q.Offset(0, -4).Value ' colonne Y
            arrLstBox(Ligne_Listbox, 6) = q.Offset(0, 1).Value ' colonne AD
            arrLstBox(Ligne_Listbox, 7) = q.Offset(0, 2).Value ' colonne AE
            arrLstBox(Ligne_Listbox, 8) = q.Offset(0, 3).Value ' colonne AF
            arrLstBox(Ligne_Listbox, 9) = q.Offset(0, 4).Value ' colonne AG
            '..... et on continue
     
            Ligne_Listbox = Ligne_Listbox + 1
            Set q = Range("C9:C" & Cells(Rows.Count, "C").End(xlUp).Row).FindNext(q)
     
        Loop While Not q Is Nothing And q.Address <> F
     
        ListBox5.List = arrLstBox()
     
    End Sub

  3. #3
    Membre averti
    Homme Profil pro
    employé de bureau
    Inscrit en
    Août 2014
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : employé de bureau
    Secteur : Transports

    Informations forums :
    Inscription : Août 2014
    Messages : 16
    Par défaut
    bonjour gnain et merci pour ta réponse
    elle aurait été parfaite si je connaissait le nombre de colonnes et de lignes mais le nombre de colonnes et de lignes peuvent varier.
    il faudrait que j'arrive à alimenter un tableau mais dynamique du genre Dim arrLstBox() par le nombre de lignes entières trouvées. tu crois que c'est possible ?

  4. #4
    Membre émérite
    Homme Profil pro
    Programmeur analyste
    Inscrit en
    Février 2009
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : Canada

    Informations professionnelles :
    Activité : Programmeur analyste
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2009
    Messages : 546
    Par défaut
    Voir
    ReDim pour redimensionné le tableau à volonté

  5. #5
    Membre averti
    Homme Profil pro
    employé de bureau
    Inscrit en
    Août 2014
    Messages
    16
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations professionnelles :
    Activité : employé de bureau
    Secteur : Transports

    Informations forums :
    Inscription : Août 2014
    Messages : 16
    Par défaut
    il faudrait donc que je fasse
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    redim arrlistbox (1 to q.row, 1 to dercol)
    dans la boucle do où dercol représente la dernière colonne non vide. C'est çà ?

  6. #6
    Membre émérite
    Homme Profil pro
    Programmeur analyste
    Inscrit en
    Février 2009
    Messages
    546
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 54
    Localisation : Canada

    Informations professionnelles :
    Activité : Programmeur analyste
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2009
    Messages : 546
    Par défaut
    Bonjour,
    comme cela

    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
     
        Dim arrLstBox()
        ListBox5.ColumnCount = 50
        Ligne_Listbox = 1
        Set q = Range("AC9:AC" & Cells(Rows.Count, "AC").End(xlUp).Row).Find("Obsolète")
        F = q.Address
        Do
     
            Ligne_Listbox = Ligne_Listbox + 1
            Set q = Range("AC9:AC" & Cells(Rows.Count, "AC").End(xlUp).Row).FindNext(q)
            ReDim arrLstBox(1 To Ligne_Listbox, 1 To dercol)
        Loop While Not q Is Nothing And q.Address <> F
     
        Ligne_Listbox = 1
     
        Do
     
            arrLstBox(Ligne_Listbox, 1) = q.Value
            arrLstBox(Ligne_Listbox, 2) = q.Offset(0, -1).Value ' colonne AB
            arrLstBox(Ligne_Listbox, 3) = q.Offset(0, -2).Value ' colonne AA
            arrLstBox(Ligne_Listbox, 4) = q.Offset(0, -3).Value ' colonne Z
            arrLstBox(Ligne_Listbox, 5) = q.Offset(0, -4).Value ' colonne Y
            arrLstBox(Ligne_Listbox, 6) = q.Offset(0, 1).Value ' colonne AD
            arrLstBox(Ligne_Listbox, 7) = q.Offset(0, 2).Value ' colonne AE
            arrLstBox(Ligne_Listbox, 8) = q.Offset(0, 3).Value ' colonne AF
            arrLstBox(Ligne_Listbox, 9) = q.Offset(0, 4).Value ' colonne AG
            '.....ainsi de suite
     
            Ligne_Listbox = Ligne_Listbox + 1
            Set q = Range("AC9:AC" & Cells(Rows.Count, "AC").End(xlUp).Row).FindNext(q)
     
        Loop While Not q Is Nothing And q.Address <> F
     
        ListBox5.List = arrLstBox()

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

Discussions similaires

  1. Aide pour Macro VBA copie lignes entre 2 classeur
    Par magicsismic dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 30/03/2015, 21h13
  2. Recherche aide pour macro
    Par piierock dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 26/01/2015, 21h30
  3. besoin d'aide pour macro test de cellule et copie selon cas
    Par tibofo dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 03/11/2008, 00h15
  4. [VBA-E][débutant]aide pour macro sous excel
    Par julyBL dans le forum Macros et VBA Excel
    Réponses: 12
    Dernier message: 09/06/2006, 22h42
  5. [VBA-E] aide pour macro sur excel
    Par letoulouzin31 dans le forum Macros et VBA Excel
    Réponses: 21
    Dernier message: 24/05/2006, 11h29

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