Vasilis Ioannidis
New Member
- Joined
- Sep 12, 2016
- Messages
- 8
Hello,
I have this problem. With a table like this (75 rows):
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]Station 1[/TD]
[TD]Station 2[/TD]
[TD]Station 3[/TD]
[TD]Station 4[/TD]
[TD]Station 5[/TD]
[TD]Station 6[/TD]
[/TR]
[TR]
[TD]Person 1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Person 2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I need to assign turns to each person so they can go through all stations. The maximum people per station can be 14 so we need 6 turns. It is important that the order assigned is random. I have developed this macro to help me through which works fine for the first column (I am new in VBA so any suggestion on how to make this code better is more than welcome). My problem is that when I run it for the next columns I don't want to get duplicate values in a row (e.g. Person 1 to have the value "Turn 1" more than once). Do you have any idea how I can make this work?
....................................................................
Sub grouping()
Dim n As Integer, i As Integer, j As Integer, c As Integer, sample As Integer, counter As Integer
Dim indexes As New Collection
Dim rng As Range
c = 2
n = 14
Math.Randomize
For i = 2 To 76
If IsEmpty(Cells(i, c)) Then
indexes.Add (i)
End If
Next i
For j = 1 To 6
counter = 0
While counter < 14 And indexes.Count > 0
sample = Int(Math.Rnd() * indexes.Count + 1)
If IsEmpty(Cells(indexes.Item(sample), c)) Then
Cells(indexes.Item(sample), c).Value = "Turn" & " " & j
indexes.Remove (sample)
counter = counter + 1
Else
indexes.Remove (sample)
End If
Wend
If (counter < 14) Then
MsgBox "Not all positions free"
End If
Next j
End Sub
.......................................................
Thank you!
I have this problem. With a table like this (75 rows):
[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]Station 1[/TD]
[TD]Station 2[/TD]
[TD]Station 3[/TD]
[TD]Station 4[/TD]
[TD]Station 5[/TD]
[TD]Station 6[/TD]
[/TR]
[TR]
[TD]Person 1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Person 2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
I need to assign turns to each person so they can go through all stations. The maximum people per station can be 14 so we need 6 turns. It is important that the order assigned is random. I have developed this macro to help me through which works fine for the first column (I am new in VBA so any suggestion on how to make this code better is more than welcome). My problem is that when I run it for the next columns I don't want to get duplicate values in a row (e.g. Person 1 to have the value "Turn 1" more than once). Do you have any idea how I can make this work?
....................................................................
Sub grouping()
Dim n As Integer, i As Integer, j As Integer, c As Integer, sample As Integer, counter As Integer
Dim indexes As New Collection
Dim rng As Range
c = 2
n = 14
Math.Randomize
For i = 2 To 76
If IsEmpty(Cells(i, c)) Then
indexes.Add (i)
End If
Next i
For j = 1 To 6
counter = 0
While counter < 14 And indexes.Count > 0
sample = Int(Math.Rnd() * indexes.Count + 1)
If IsEmpty(Cells(indexes.Item(sample), c)) Then
Cells(indexes.Item(sample), c).Value = "Turn" & " " & j
indexes.Remove (sample)
counter = counter + 1
Else
indexes.Remove (sample)
End If
Wend
If (counter < 14) Then
MsgBox "Not all positions free"
End If
Next j
End Sub
.......................................................
Thank you!