Select 5 random values in a dynamic range

mista_v

New Member
Joined
Jul 30, 2015
Messages
11
Hi all,

I'm using the following code to select 5 random numbers from D2 to D50 and then posting those 5 random numbers starting in D52. My problem is that sometimes there are only 15 rows with data in the range. So this macro also selects blank cells. Anyone an idea how to modify the code I'm using? Thank you!

Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")

Const outputCount As Long = 5 'Number of output you want
Const outputFirstCell As String = "D52" 'Address of the output first cell

Const numStartRow As Long = 2 'First row of the list of numbers
Const numEndRow As Long = 50 'Last row of the list of numbers
Const numColumn As Long = 4 'Column index of the list of numbers

'Store the values of the list of numbers in an array (to avoid reading from cells unnecessary)
Dim numList As Variant
numList = ws.Range(ws.Cells(numStartRow, numColumn), ws.Cells(numEndRow, numColumn)).Value

Dim uniqueNum As Object
Set uniqueNum = CreateObject("Scripting.Dictionary")

'Generate a random number and add the corresponding value in dictionary if it does not exist, stop once it has 30 entires
Do While uniqueNum.Count <> outputCount
Dim inputVal As Long
inputVal = Application.RandBetween(1, UBound(numList))

If Not uniqueNum.Exists(numList(inputVal, 1)) Then
uniqueNum.Add (numList(inputVal, 1)), 1
End If
Loop

Dim uniqueList As Variant
uniqueList = uniqueNum.Keys

'Write output to worksheet
ws.Range(outputFirstCell).Resize(outputCount).Value = Application.WorksheetFunction.Transpose(uniqueList)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
You just need to change how you define numendrow
VBA Code:
'Const numEndRow As Long = 50 'Last row of the list of numbers change this line to:
Dim numEndRow As Long
numEndRow = Cells(Rows.Count, numColumn).End(xlUp).Row
You also need to move it after the row
VBA Code:
Const numColumn As Long = 4 'Column index of the list of numbers
 
Upvote 0
Solution

Forum statistics

Threads
1,223,935
Messages
6,175,496
Members
452,649
Latest member
mr_bhavesh

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