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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,223,923
Messages
6,175,392
Members
452,640
Latest member
steveridge

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