1

I want to create a macro that will essentially return random numbers based on a users input, however, I want each output to be unique (which is why the randbetween() function won't work for this). Below is what I have so far, but I keep getting a reference error. I have stitched this code together from a few different examples I found online so optimizing in any way would also be appreciated.

Code:

Sub RandomSample()
Dim cell As Range
Dim rng As Range
Low = 1
High = Application.InputBox("Enter population total", Type:=1)
Sample = Application.InputBox("Enter the Sample Size", Type:=8)
Set rng = Application.Range(ActiveCell, ActiveCell.Offset(Sample, 0))
For Each cell In rng.Cells
    If WorksheetFunction.CountA(Selection) = (High - Low + 1) Then Exit For
    Do
        rndNumber = Int((High - Low + 1) * Rnd() + Low)
    Loop Until Selection.Cells.Find(rndNumber, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
    cell.Value = rndNumber
Next
End Sub

error window: Error image

0

3 Answers 3

3

Try this

Sub RandomSample()
    Dim cell As Range
    Dim Sample As Range  'declare Sample as Range
    Low = 1
    High = Application.InputBox("Enter population total", Type:=1)
    Set Sample = Application.InputBox("Enter the Sample Size", Type:=8)
    For Each cell In Sample   'use sample in loop
        If WorksheetFunction.CountA(Sample) = (High - Low + 1) Then Exit For
        Do
            rndnumber = Int((High - Low + 1) * Rnd() + Low)
        Loop Until Sample.Cells.Find(rndnumber, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
        cell.Value = rndnumber
    Next
End Sub

EDIT :

Sub RandomSample()
    Dim cell As Range
    Dim rng As Range
    Dim High As Long, Sample As Long
    Low = 1
    High = Application.InputBox("Enter population total", Type:=1)
    Sample = Application.InputBox("Enter the Sample Size", Type:=1)
    Set rng = Application.Range(ActiveCell, ActiveCell.Offset(Sample, 0))
    For Each cell In rng.Cells
        If WorksheetFunction.CountA(rng) = (High - Low + 1) Then Exit For
        Do
            rndNumber = Int((High - Low + 1) * Rnd() + Low)
        Loop Until rng.Cells.Find(rndNumber, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
        cell.Value = rndNumber
    Next
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

@Jeeped - Missed it, thanks for pointing it out, answer updated.
you may also want to see the other answer about the type:= error.
@ScottCraner - Seems like I went completely in wrong direction.
I was still getting the error with this code, kurtz suggestion of changing the Sample type worked for me.
@Melk - I've added another piece of code, you might check.
|
3

Here is a formula that does what you want:

=IF(ROW(1:1)<=$B$1,INDEX(ROW(INDIRECT("1:" & $A$1)),AGGREGATE(15,6,ROW(INDIRECT("1:" &$A$1))/(COUNTIF($A$2:A2,ROW(INDIRECT("1:" & $A$1)))=0),RANDBETWEEN(1,$A$1-COUNT($A$2:A2)))),"")

Where A1 is the population and B1 is the Sample size

enter image description here

Comments

2

Your Sample uses Type:=8 which is a Range and then you're trying to use it as a "number" in an "Offset" function. Change this line:

Sample = Application.InputBox("Enter the Sample Size", Type:=8)

To:

Sample = Application.InputBox("Enter the Sample Size", Type:=1)

1 Comment

This does not address the abstract use of Selection in the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.