VBA Random Samling, No Duplication. Inputs: Population Size, Sample Size

vtidus

New Member
Joined
Dec 14, 2017
Messages
4
Hello everyone,

I performed a small research but could not identify a code for my need.

I want to create a sampling file in excel, that will have the population size in cell b2, and the required sample size in cell b5. (The required sample is to be up to 40.)

I want to create a macro that will read the population size and sample (from 1 to the population size) random numbers by the required times (cell b5) without duplication. The output of the random numbers is to be included in the area of f2:f41 (If required sample is less than 40, e.g. 5, then only fill cells f2:f6).

Could you please assist me?

Thank you very much for your input.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I've managed to create the following code, but I have yet to find a solution to not generate duplicates:

Code:
Sub Random_Sampling()
Dim LR As Integer
Dim i As Integer
Dim w As Integer
Worksheets("Random Sampling").Activate
Do Until i = Range("B5").Value
w = w + 1
Randomize
LR = Int((Range("B2").Value + 1) * Rnd + 1)
Cells(2 + i, 6).Value = LR
i = i + 1
Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,789
Messages
6,121,590
Members
449,039
Latest member
Arbind kumar

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