+ Reply to Thread
Results 1 to 5 of 5

random UNique Dict

Hybrid View

  1. #1
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    random UNique Dict

    i have code random unique number star from 1 until 15
    my code is sukses use dictionary with loop while
    but in this moment i want modifikasi with for or for each what must code change
    this my code
    Sub RandomDictionary()
    Dim n&, Max&, Min&
    Max = 15: Min = 1
    With CreateObject("scripting.dictionary")
        Do While .Count <= Max - Min
           n = Rnd() * (Max - Min) + Min
           .Item(n) = x0
        Loop
       [a1:a15] = Application.Transpose(.keys)
    End With
    End Sub
    it is sucses but i want to change loop use for or for each what must to do this bellow my code not runing perfect
    Sub RandomDictionary()
    Dim n&, Max&, Min&,i&
    Max = 15: Min = 1
    With CreateObject("scripting.dictionary")
        for i = 1 to 14
            n = Rnd() * (Max - Min) + Min
           .Item(n) = x0
        next i
       [a1:a15] = Application.Transpose(.keys)
    End With
    End Sub

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: random UNique Dict

    That method is not the most efficient. I guess it might go something like:
    Sub RandomDictionary()
      Dim n&, Max&, Min&, i&
      Max = 15: Min = 1
      With CreateObject("scripting.dictionary")
        Do Until .Count = Max
          i = i + 1
          n = Rnd() * (Max - Min) + Min
          .Item(n) = i
        Loop
        [a1:a15] = Application.Transpose(.keys)
      End With
    End Sub

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: random UNique Dict

    Fist code is better. But if for some reason you DO want to use for ... next, you can do it for instance this way:
    Sub RandomDictionary1()
    Dim n&, Max&, Min&, i&
    Max = 15: Min = 1
    With CreateObject("scripting.dictionary")
        For i = 1 To 15
          Do
            n = Rnd() * (Max - Min) + Min
          Loop Until Not .exists(n)
           .Item(n) = x0
        Next i
       [a1:a15] = Application.Transpose(.keys)
    End With
    End Sub
    Best Regards,

    Kaper

  4. #4
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,513

    Re: random UNique Dict

    Thank you Kaper

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: random UNique Dict

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VB Code to create DICT
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-27-2017, 07:30 AM
  2. Replies: 2
    Last Post: 11-06-2014, 05:07 PM
  3. Script dict displaying one row instead of all applicable rows ?
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-02-2014, 02:58 PM
  4. Unique random numbers
    By Bartlett in forum Excel General
    Replies: 2
    Last Post: 01-28-2012, 03:42 PM
  5. Random and Unique Groups
    By rosslo189 in forum Excel General
    Replies: 1
    Last Post: 05-14-2011, 05:55 PM
  6. random unique numbers
    By Juli in forum Excel General
    Replies: 2
    Last Post: 05-05-2011, 05:48 PM
  7. Unique random dates using VBA
    By mainemojo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-22-2009, 03:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1