youbitto
New Member
- Joined
- Jun 8, 2022
- Messages
- 34
- Office Version
- 2019
- 2016
- 2013
- Platform
- Windows
I have two sheets
first is "Inscrp" where I put the names depending on categories in different columns Second is "Tirage" where I see random names appears from each category
HowMany = 5 in the code represent the number of names to be picked randomly The problem when I write less than 5 names the result would be empty.
I want the names to be picked randomly regardless of their number and prevent Hardcoding the "HowMany"
This is the code for it
Like this it returns only 5 names randomly from each filled column
Illustration :
In this case the column B in "Tirage" return empty because the HowMany I assigned is 5 names
------------------------------------Sheet1"Inscrp"-------------------------------------------------------------------------------------Sheet2"Tirage"
------------------------------------Sheet1"Inscrp"-------------------------------------------------------------------------------------Sheet2"Tirage"
first is "Inscrp" where I put the names depending on categories in different columns Second is "Tirage" where I see random names appears from each category
HowMany = 5 in the code represent the number of names to be picked randomly The problem when I write less than 5 names the result would be empty.
I want the names to be picked randomly regardless of their number and prevent Hardcoding the "HowMany"
This is the code for it
VBA Code:
Sub PickNamesAtRandom()
Dim shI As Worksheet, lastR As Long, shT As Worksheet, HowMany As Long
Dim rndNumber As Integer, Names() As String, i As Long, CellsOut As Long
HowMany = 5: CellsOut = 8
Set shI = Worksheets("Inscrp")
Set shT = Worksheets("Tirage")
Dim col As Long, arrCol, filt As String, nrCol As Long
nrCol = shT.Cells(4, 8) 'number of columns to be returned. It can be changed and also be calculated...
For col = 1 To nrCol
lastR = shI.Cells(shI.Rows.Count, col).End(xlUp).Row 'last row in column to be processed
If lastR >= HowMany + 2 Then '+ 2 because the range is build starting with the third row...
arrCol = Application.Transpose(shI.Range(shI.Cells(3, col), shI.Cells(lastR, col)).Value2) 'place the range in a 1D array
ReDim Names(1 To HowMany) 'Set the array size to how many names required
For i = 1 To UBound(Names)
tryAgain:
Randomize
rndNumber = Int((UBound(arrCol) - LBound(arrCol) + 1) * Rnd + LBound(arrCol))
If arrCol(rndNumber) = "" Then GoTo tryAgain
Names(i) = arrCol(rndNumber)
filt = arrCol(rndNumber) & "##$$@": arrCol(rndNumber) = filt
arrCol = Filter(arrCol, filt, False) 'eliminate the already used name from the array
Next i
shT.Cells(CellsOut, col).Resize(UBound(Names), 1).Value2 = Application.Transpose(Names)
End If
Next col
MsgBox "Ready..."
End Sub
Like this it returns only 5 names randomly from each filled column
Illustration :
In this case the column B in "Tirage" return empty because the HowMany I assigned is 5 names
------------------------------------Sheet1"Inscrp"-------------------------------------------------------------------------------------Sheet2"Tirage"
A | B | | A | B |
---|---|---|---|---|
John | Simon | | David | "Nothing" |
David | Gerard | | Steve | |
Jacob | Herald | | john | |
Steve | Paul | | Sara | |
Sara | | | Jacob | |
This is how I want it :------------------------------------Sheet1"Inscrp"-------------------------------------------------------------------------------------Sheet2"Tirage"
A | B | | A | B |
---|---|---|---|---|
John | Simon | | David | Gerard |
David | Gerard | | Steve | Paul |
Jacob | Herald | | john | Simon |
Steve | Paul | | Sara | Herald |
Sara | | | Jacob | |