denismccarthy
Board Regular
- Joined
- Dec 30, 2006
- Messages
- 108
I have a macro that picks a random team ( so I was hoping), but everytime I open the spreadsheet, the same team appears. It's suppose to pick a random number in the a drop down list.
Am I using the rand() function wrong?
Heres the line
x = Rnd() * (PlayerNo(i) - 1) + 1
x = rnd()* (38 -1)+1
x = 14
Any help would be appreciated
Sub QuickPick()
Dim x As Integer
Dim i As Integer
ReDim PlayerNo(11) As Integer
ReDim Player(11) As String
Dim Player1 As String
Dim Player2 As String
Dim Player3 As String
Dim Player4 As String
Dim Player5 As String
Dim Player6 As String
Dim Player7 As String
Dim Player8 As String
Dim Player9 As String
Dim Player10 As String
Dim Player11 As String
Dim PlayerNo1 As Double
Dim PlayerNo2 As Double
Dim PlayerNo3 As Double
Dim PlayerNo4 As Double
Dim PlayerNo5 As Double
Dim PlayerNo6 As Double
Dim PlayerNo7 As Double
Dim PlayerNo8 As Double
Dim PlayerNo9 As Double
Dim PlayerNo10 As Double
Dim PlayerNo11 As Double
Dim c As Integer
PlayerNo(1) = 19
PlayerNo(2) = 116
PlayerNo(3) = 116
PlayerNo(4) = 116
PlayerNo(5) = 116
PlayerNo(6) = 127
PlayerNo(7) = 127
PlayerNo(8) = 127
PlayerNo(9) = 127
PlayerNo(10) = 80
PlayerNo(11) = 80
Player(1) = "Goalkeeper"
Player(2) = "Defender1"
Player(3) = "Defender2"
Player(4) = "Defender3"
Player(5) = "Defender4"
Player(6) = "Midfielder1"
Player(7) = "Midfielder2"
Player(8) = "Midfielder3"
Player(9) = "Midfielder4"
Player(10) = "Striker1"
Player(11) = "Striker2"
Reset:
Application.Run "Macro2"
For i = 1 To 11
RestartPick:
x = Rnd() * (PlayerNo(i) - 1) + 1
If x > PlayerNo(i) Then GoTo RestartPick
Sheets("Form").Select
Worksheets("Form").Shapes("Drop Down " & i).OLEFormat.Object.Value = x + 1
If i >= 2 Then
For c = 1 To (i - 1)
If Worksheets("Form").Shapes("Drop Down " & i).OLEFormat.Object.Value = Worksheets("Form").Shapes("Drop Down " & c).OLEFormat.Object.Value Then
GoTo RestartPick
Else
End If
Next
Application.Run Player(i)
If WorksheetFunction.Max(Sheets("PrintForm").Range("k1:k40")) > 3 Then
GoTo RestartPick
Else
End If
Else
End If
Application.Run Player(i)
Next
Range("i6").Select
If Selection < 0 Then GoTo Reset
Range("c4").Select
End Sub
Am I using the rand() function wrong?
Heres the line
x = Rnd() * (PlayerNo(i) - 1) + 1
x = rnd()* (38 -1)+1
x = 14
Any help would be appreciated
Sub QuickPick()
Dim x As Integer
Dim i As Integer
ReDim PlayerNo(11) As Integer
ReDim Player(11) As String
Dim Player1 As String
Dim Player2 As String
Dim Player3 As String
Dim Player4 As String
Dim Player5 As String
Dim Player6 As String
Dim Player7 As String
Dim Player8 As String
Dim Player9 As String
Dim Player10 As String
Dim Player11 As String
Dim PlayerNo1 As Double
Dim PlayerNo2 As Double
Dim PlayerNo3 As Double
Dim PlayerNo4 As Double
Dim PlayerNo5 As Double
Dim PlayerNo6 As Double
Dim PlayerNo7 As Double
Dim PlayerNo8 As Double
Dim PlayerNo9 As Double
Dim PlayerNo10 As Double
Dim PlayerNo11 As Double
Dim c As Integer
PlayerNo(1) = 19
PlayerNo(2) = 116
PlayerNo(3) = 116
PlayerNo(4) = 116
PlayerNo(5) = 116
PlayerNo(6) = 127
PlayerNo(7) = 127
PlayerNo(8) = 127
PlayerNo(9) = 127
PlayerNo(10) = 80
PlayerNo(11) = 80
Player(1) = "Goalkeeper"
Player(2) = "Defender1"
Player(3) = "Defender2"
Player(4) = "Defender3"
Player(5) = "Defender4"
Player(6) = "Midfielder1"
Player(7) = "Midfielder2"
Player(8) = "Midfielder3"
Player(9) = "Midfielder4"
Player(10) = "Striker1"
Player(11) = "Striker2"
Reset:
Application.Run "Macro2"
For i = 1 To 11
RestartPick:
x = Rnd() * (PlayerNo(i) - 1) + 1
If x > PlayerNo(i) Then GoTo RestartPick
Sheets("Form").Select
Worksheets("Form").Shapes("Drop Down " & i).OLEFormat.Object.Value = x + 1
If i >= 2 Then
For c = 1 To (i - 1)
If Worksheets("Form").Shapes("Drop Down " & i).OLEFormat.Object.Value = Worksheets("Form").Shapes("Drop Down " & c).OLEFormat.Object.Value Then
GoTo RestartPick
Else
End If
Next
Application.Run Player(i)
If WorksheetFunction.Max(Sheets("PrintForm").Range("k1:k40")) > 3 Then
GoTo RestartPick
Else
End If
Else
End If
Application.Run Player(i)
Next
Range("i6").Select
If Selection < 0 Then GoTo Reset
Range("c4").Select
End Sub