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


New Member
Dec 14, 2017
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.


New Member
Dec 14, 2017
I've managed to create the following code, but I have yet to find a solution to not generate duplicates:

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
LR = Int((Range("B2").Value + 1) * Rnd + 1)
Cells(2 + i, 6).Value = LR
i = i + 1
End Sub

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...