I am new to excel vba... The code is below is using the nested if statement to display the two random number based on the user input say one or two. I am getting a compile error where the else statement is. The first if statement works but cannot get the second one to work...any ideas?
Help is appreciated
here is the code:
Dim Random1 As Integer, Random2 As Integer, Random3 As Integer 'declare varible for random number
Dim Random
Randomize ' generate new random number
'Generating random number 1
For i = 10 To 100
Random1 = Int((100 - 10 + 1) * Rnd + 10)
Next i
'generating random number 2
For i = 10 To 100
Random2 = Int((100 - 10 + 1) * Rnd + 10)
Next i
'Input box which allows the user to input a number
Random = Val(InputBox("Enter the Random Number(1 or 2) you would like to select: ", "Random Number"))
'If Random < 1 Or Random > 2 Then
'MsgBox ("Random Number must be 1 or 2")
'End If
If Random = 1 Then
If Random1 > Random2 Then
MsgBox ("win")
ElseIf Random1 < Random2 Then
MsgBox ("lose")
End If
End If
Else
If Random = 2 Then
If Random2 > Random1 Then
MsgBox ("win")
ElseIf Random2 < Random1 Then
MsgBox ("lose")
End If
End If
'the message will display the random number generated for number 1 and number 2 plus the Random Number selected
'either 1 or 2 to determine if we win or lose.
MsgBox ("Random number 1 is " & Random1 & " and Random number 2 is " & Random2 & " and you selected random number " & Random)
End Sub
Bookmarks