Function MergeAndSortArrays(arr1() As Variant, arr2() As Variant, Optional tri As Boolean = True, Optional Decrm As Boolean = False) As Variant
'**************************************************************************************************************************
'Fusionne 2 matrices de longueurs différentes et tri le contenu
'https://stackoverflow.com/questions/1588913/how-do-i-merge-two-arrays-in-vba (pour la fusion des Arrays)
'dysorthographie (pour le tri de l'Array résultant)
'- arr1 : 1ère matrice
'- arr2 : 2ème matrice
'- tri : si True (ou omis) --> tri des 2 matrices fusionnées
' si False --> pas de tri
'- Decrm : si False (ou omis) --> tri croissant
' si True --> tri décroissant
'Ex : arr1 = Array("jaune", "vert", "orange") | arr2 = Array("blanc", "rouge", "noir", "fuchsia", "marron")
' MergeAndSortArrays(arr1, arr2, False) --> "jaune", "vert", "orange", "blanc", "rouge", "noir", "fuchsia", "marron"
' MergeAndSortArrays(arr1, arr2) --> "blanc", "fuchsia", "jaune", "marron", "noir", "orange", "rouge", "vert"
' MergeAndSortArrays(arr1, arr2, , True) --> "vert", "rouge", "orange", "noir", "marron", "jaune", "fuchsia", "blanc"
'**************************************************************************************************************************
Dim returnThis() As Variant, len1 As Integer, len2 As Integer, lenRe As Integer, counter As Integer
Dim i&, temp
len1 = UBound(arr1)
len2 = UBound(arr2)
lenRe = len1 + len2
ReDim returnThis(1 To lenRe)
counter = 1
'Fusion des 2 matrices
Do While counter <= len1 'get first array in returnThis
returnThis(counter) = arr1(counter)
counter = counter + 1
Loop
Do While counter <= lenRe 'get the second array in returnThis
returnThis(counter) = arr2(counter - len1)
counter = counter + 1
Loop
'Tri de la matrice résultante
If tri Then
For i = LBound(returnThis) + 1 To UBound(returnThis)
If returnThis(i + Decrm) < returnThis(i + Not Decrm) Then
temp = returnThis(i + Not Decrm): returnThis(i + Not Decrm) = returnThis(i + Decrm): returnThis(i + Decrm) = temp
i = i - 2
If i < 0 Then i = 0
End If
Next
End If
MergeAndSortArrays = returnThis
End Function