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
   | Sub CopieDynamique()
'la première étape est de mettre les données dans une table
Dim tab_infos()
Dim nb_ligne_Infos, Nb_Colonne_Infos, Lig
Dim wshSrc As Worksheet
Dim wshDst As Worksheet
Dim t1, t2 As Double
t1 = Timer
Set wshDst = ThisWorkbook.Worksheets("Test")
 
Set wshSrc = ThisWorkbook.Worksheets("Feuille")  '.Activate
nb_ligne_Infos = wshSrc.Range("A" & wshSrc.Rows.Count).End(xlUp).Row
Nb_Colonne_Infos = wshSrc.Cells(1, wshSrc.Columns.Count).End(xlToLeft).Column
tab_infos = wshSrc.Range(wshSrc.Cells(2, 1), wshSrc.Cells(nb_ligne_Infos, Nb_Colonne_Infos))
 
Dim b As Variant
Dim v As Integer
Dim tab_intern()
 
v = 1
 
'on parcourt chaque ligne de la table
 
For Lig = 1 To nb_ligne_Infos - 1
 
    'on vérifie si on trouve des correspondances dans les colonnes 5 et 1
 
    If CStr(tab_infos(Lig, 2)) = "tartempion4" And CStr(tab_infos(Lig, 3)) = "bidule01" Then
        'si oui, on stocke le résultat des colonnes 4 et 5 dans la table tab_intern (j'utilise une table pour stocker mes résultats car donc mon cas réel c'est plus pratique)
 
        ReDim Preserve tab_intern(1 To 2, 1 To v)
 
        tab_intern(1, v) = tab_infos(Lig, 4)
        tab_intern(2, v) = tab_infos(Lig, 5)
 
        ' Je copie les données sur une feuille excel pour vérifier
 
        'wshDst.Cells(v, 1).Value = tab_intern(1, v)
        'wshDst.Cells(v, 2).Value = tab_intern(2, v)
 
        'on incrémente tab_intern s'il y a plusieurs lignes dans tab_infos qui correspond à notre requête
        v = v + 1
    End If
Next 'Lig
 ' Je copie les données sur une feuille excel pour vérifier
Array2Range tab_intern, wshDst.Range("A1"), True
t2 = Timer
Debug.Print "Temps écoulé :", CStr(t2 - t1) + " s"
End Sub
 
Sub Array2Range(arr, destTL As Range, transpose As Boolean)
    If transpose Then arr = Application.WorksheetFunction.transpose(arr)
    'dumps  (1D/2D) onto a sheet where [destTL] is the top-left output cell.
    destTL.Resize(UBound(arr, 1) - LBound(arr, 1) + 1, _
        UBound(arr, 2) - LBound(arr, 2) + 1) = arr
End Sub | 
Partager