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
Bookmarks