Simple Randomizer

Bryancarlo

New Member
Joined
Apr 13, 2021
Messages
21
Hi,

Im trying to create a macro based randomizer. I wish to randomize samples in the "Population" sheet (Column A). After clicking "Randomize", random samples based from Required Sample % or Required Sample Count will be pasted in the "Samples" sheet Column A. Numbers in Required Sample % and Required Sample Count are manually entered depending on the need.

Hoping for your help.

1693424772038.png
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Let say you have 20 items. %20 means, you are going to pick 4 items. But Count says 5. Which rule to pick? I am confused.
 
Upvote 0
Let say you have 20 items. %20 means, you are going to pick 4 items. But Count says 5. Which rule to pick? I am confused.
Sorry bout the confusion. Either we do Required Sample % or Required Sample Count. We cant use both at the same time
 
Upvote 0
One last question. Are you entering %20 as "0,20" and later formatting as perfentage? Or is it directly number 20?

I hope I am clear.
 
Upvote 0
Hello!

Sory for the late reply. This should work for you:
VBA Code:
Sub test()
  Dim myRange As Variant, percentage As Integer, c As Integer, i As Long, rnd As Integer
  With Worksheets("Sheet1")
  myRange = .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
  percentage = .Range("C2").Value
  c = IIf(.Range("C2").Value > 0, UBound(myRange, 1) * (percentage / 100), .Range("D2").Value)
  End With
  With Worksheets("Sheet2")
  .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Row).Clear
  For i = 1 To c
    Do
      rnd = WorksheetFunction.RandBetween(1, UBound(myRange, 1))
    Loop While Not IsError(Application.Match(myRange(rnd, 1), .Range("A:A"), 0))
    .Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = myRange(rnd, 1)
  Next
  End With
End Sub
 
Upvote 0
Hello!

Sory for the late reply. This should work for you:
VBA Code:
Sub test()
  Dim myRange As Variant, percentage As Integer, c As Integer, i As Long, rnd As Integer
  With Worksheets("Sheet1")
  myRange = .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Row)
  percentage = .Range("C2").Value
  c = IIf(.Range("C2").Value > 0, UBound(myRange, 1) * (percentage / 100), .Range("D2").Value)
  End With
  With Worksheets("Sheet2")
  .Range("A2:A" & .Cells(Rows.Count, "A").End(xlUp).Row).Clear
  For i = 1 To c
    Do
      rnd = WorksheetFunction.RandBetween(1, UBound(myRange, 1))
    Loop While Not IsError(Application.Match(myRange(rnd, 1), .Range("A:A"), 0))
    .Cells(Rows.Count, "A").End(xlUp).Offset(1).Value = myRange(rnd, 1)
  Next
  End With
End Sub
oh my God! It works perfectly! Thank you very much!!
 
Upvote 0

Forum statistics

Threads
1,215,793
Messages
6,126,937
Members
449,349
Latest member
Omer Lutfu Neziroglu

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