Hi
I am tasked with creating a method via vba that will randomly pick staff ids and department names from a list - I have attached a sample document together with VBA code within the document.
I need to get exactly 12.5% return from each department. Using basic formulas and a pivot table I can determine what 12.5% of the each department would come to however I am unsure as to how to randomly pick exactly 12.5% of these so that the vba would split each by a random 12.5% and return precisely that number.
At present I am using the following vba ...
Using this I would filter the main data sheet by department, paste that data onto sheet 1 and on sheet 2 run the random number picker - which should return exactly 12.5% of the numbers and departments with no duplicates picked at random - it doesn't do that at present. After I have done that I would copy and paste each time because I am building a complete document with 5,000 random employee ids and departments in it.
Actually it would be great to have a document that could pick 12.5% at random each department and with no duplicates and create a complete list of the 5,000 employee id's and departments with no duplicates in - instead of cutting and pasting each time to form a list of 5000 employee ids and departments.
Your advice would be welcomed.
Kind Regards
Ben Howard
I am tasked with creating a method via vba that will randomly pick staff ids and department names from a list - I have attached a sample document together with VBA code within the document.
I need to get exactly 12.5% return from each department. Using basic formulas and a pivot table I can determine what 12.5% of the each department would come to however I am unsure as to how to randomly pick exactly 12.5% of these so that the vba would split each by a random 12.5% and return precisely that number.
At present I am using the following vba ...
Code:
Sub random50()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rng As Range
Dim lr As Long
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
Set ws2 = ThisWorkbook.Worksheets("Sheet2")
lr = ws1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = ws1.Range("A1:A" & lr)
For i = 1 To 94
ws2.Range("A" & i) = Application.WorksheetFunction.Index(rng, Application.WorksheetFunction.RandBetween(1, lr))
Next i
End Sub
Actually it would be great to have a document that could pick 12.5% at random each department and with no duplicates and create a complete list of the 5,000 employee id's and departments with no duplicates in - instead of cutting and pasting each time to form a list of 5000 employee ids and departments.
Your advice would be welcomed.
Kind Regards
Ben Howard
Attachments
Last edited by a moderator: