Results 1 to 4 of 4

Thread: Random Unique Value Generator Based On Sample Size And Maximum Items Limit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Paste this to the code module of the respective sheet and run the macro.

    Code:
    Sub RandLookUp()
    
        Dim lng As Long
        Dim lngMax As Long: lngMax = Range("L5").Value
        Dim lngMin As Long: lngMin = Range("K5").Value
        Dim strNames(1 To 5000) As String
        
        With CreateObject("Scripting.Dictionary")
            Do While .Count <= Range("K7").Value 'lngMax - lngMin          'removed the greater than symbol
                lng = Rnd * (lngMax - lngMin) + lngMin  'removed the +1
                .Item(lng) = Empty
            Loop
            lngMin = Empty
            For lng = 1 To Range("J10:J18").Rows.Count
                If Not IsEmpty(Range("J10:J18").Cells(lng, 1)) Then
                    For lngMax = 1 To Range("J10:J18").Cells(lng, 2).Value
                        lngMin = lngMin + 1
                        strNames(lngMin) = Range("J10:J18").Cells(lng, 1).Value
                    Next lngMax
                End If
            Next lng
            Range(Range("A2"), Cells(Rows.Count, 1).End(xlUp)).Resize(, 4).ClearContents
            Range("A2").Resize(Range("K7").Value).Value = Application.Transpose(.Keys)
            Range("B2").Resize(Range("K7").Value, 2).Formula = "=VLOOKUP($A2,Sheet1!$A:B,COLUMN(),0)"
            Range("D2").Resize(Range("K7").Value).Value = Application.Transpose(strNames)
        End With
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0
    Thanks a lot Admin...

Similar Threads

  1. Replies: 10
    Last Post: 08-29-2013, 08:20 PM
  2. Replies: 14
    Last Post: 06-27-2013, 10:57 AM
  3. Unique Random Number In Ascending Order
    By marreco in forum Excel Help
    Replies: 8
    Last Post: 11-04-2012, 04:15 PM
  4. Random Unique Number Generator Excel VBA
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 5
    Last Post: 10-18-2012, 01:00 PM
  5. Create Random Number Generator VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 12-01-2011, 10:51 AM

Posting Permissions

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