trier aléatoirement mon dictionnaire

asta22

New Member
Joined
Apr 13, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
je veux trier aléatoirement mon dictionnaire, voilà mon code

que dois-je faire ?

MON CODE :

Dim key2 As Variant, ArrayDict As Variant

'création array à partir du dict
k = 1
For Each key2 In dict
ArrayDict(k, 1) = key2
ArrayDict(k, 2) = dict(key2)
k = k + 1
Next key2

' vide le dict
dict.RemoveAll

'randomise array order
For i = 1 To k 'melanger les villes candidates
a1 = aleatoire(1, k)
a2 = aleatoire(1, k)
a = ArrayDict(a1, 1): ArrayDict(a1, 1) = ArrayDict(a2, 1) : ArrayDict(a2, 1) = a
a = ArrayDict(a1, 2) : ArrayDict(a1, 2) = ArrayDict(a2, 2) : ArrayDict(a2, 2) = a
Suivant i

'on a rempli le dict à partir du tableau
For i = 1 To UBound(ArrayDict)
dict(ArrayDict(k, 1)) = ArrayDict(k, 2)
Next i

Merci d'avance
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
on met les "keys" et les "items" dans 2 arrays, puis on les ajoute dans un nouveau array avec 3 columns et dans la 3ième, c'est un random number. Puis on sort cette array dans un nouveau array et ...
VBA Code:
     mykeys = dict.keys     'read the keys in an array
     myitems = dict.items     'same for the items
     ReDim arr(UBound(mykeys), 2)     'prepare a new array (as many rows as mykeys and 3 columns (starting with column 0)
     For i = 0 To UBound(mykeys)     'fill that array
          arr(i, 0) = mykeys(i)     'with the keys
          arr(i, 1) = myitems(i)     'with the items
          arr(i, 2) = Rnd     'with a random number
     Next

     arr1 = Application.Sort(arr, 3, 1)     'sort that array with that random number ascending

     Range("D1").Resize(UBound(arr1), 2).Value = arr1     'write the result (0nly the first 2 columns) to the sheet
 
Last edited:
Upvote 0
effacer !
je pensais, que j'avait fait un erreur, non ...
 
Upvote 0
c'était rien, je voudrai effacer ma reaction, mais cela ne va pas ...
 
Upvote 0

Forum statistics

Threads
1,215,560
Messages
6,125,527
Members
449,236
Latest member
Afua

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top