XL 2016 Utiliser un dictionary pour extraire des données

KTM

XLDnaute Impliqué
Bonsoir chers tous
Dans mon fichier joint je voudrais extraire et stocker dans ma colonne AY les anciens protocoles existants en colonne AP selon les codes
J'ai adapté une macro mais qui apparemment coince un peu.
Un expert en l'usage des dictionary pourrait il me venir en aide ?
Merci
 

Pièces jointes

  • extr.xlsm
    328.8 KB · Affichages: 21

Bebere

XLDnaute Barbatruc
Bonjour
Ktm à tester
VB:
Sub extraire()
Dim dico As New Dictionary, datader&, data
Dim resultder&, result, i&, clef, deb, x
    Application.ScreenUpdating = False
   Set dico = CreateObject("scripting.dictionary")
   dico.CompareMode = 1
  
   If Me.FilterMode Then Me.ShowAllData
   datader = Cells(Rows.Count, "AO").End(xlUp).Row
   data = Cells(2, "AO").Resize(datader, 2)
  
   resultder = Cells(Rows.Count, "AT").End(xlUp).Row
   result = Range(Cells(2, "AT"), Cells(resultder, "AY")) '.Resize(resultder, 6)
   'code et ancien protocole
   For i = 1 To UBound(data): dico(data(i, 1) & " " & data(i, 2)) = data(i, 1) & " " & data(i, 2): Next
   For Each clef In dico.Keys
   For i = 1 To UBound(result)
   x = Mid(clef, 1, InStr(clef, " ") - 1)
  If result(i, 1) = x Then
  result(i, 6) = Mid(clef, InStr(clef, " ") + 1)
  End If
   Next
   Next clef
   Cells(2, "AT").Resize(UBound(result, 1), UBound(result, 2)) = result
   Set dico = Nothing
End Sub
 

KTM

XLDnaute Impliqué
Bonjour
Ktm à tester
VB:
Sub extraire()
Dim dico As New Dictionary, datader&, data
Dim resultder&, result, i&, clef, deb, x
    Application.ScreenUpdating = False
   Set dico = CreateObject("scripting.dictionary")
   dico.CompareMode = 1

   If Me.FilterMode Then Me.ShowAllData
   datader = Cells(Rows.Count, "AO").End(xlUp).Row
   data = Cells(2, "AO").Resize(datader, 2)

   resultder = Cells(Rows.Count, "AT").End(xlUp).Row
   result = Range(Cells(2, "AT"), Cells(resultder, "AY")) '.Resize(resultder, 6)
   'code et ancien protocole
   For i = 1 To UBound(data): dico(data(i, 1) & " " & data(i, 2)) = data(i, 1) & " " & data(i, 2): Next
   For Each clef In dico.Keys
   For i = 1 To UBound(result)
   x = Mid(clef, 1, InStr(clef, " ") - 1)
  If result(i, 1) = x Then
  result(i, 6) = Mid(clef, InStr(clef, " ") + 1)
  End If
   Next
   Next clef
   Cells(2, "AT").Resize(UBound(result, 1), UBound(result, 2)) = result
   Set dico = Nothing
End Sub
M E R C I

un dernier détail : j'aimerais mettre 0 si code non trouvé dans la plage source
 

Discussions similaires

Statistiques des forums

Discussions
312 273
Messages
2 086 698
Membres
103 372
dernier inscrit
BibiCh