我正在创建一个简单的问答游戏,用户点击按钮并在文本框中显示随机单词。我的问题是我如何使用随机函数来做到这一点?这是我到目前为止所得到的。
Private Sub CommandButton1_Click()
End Sub
Private Sub TextBox2_Change()
End Sub
Private Sub UserForm_Click()
Dim whitecard() As String
whitecard = ("red, blue, green, yellow, purple")
End Sub
任何帮助甚至指向正确的方向都会有所帮助。 谢谢大家。
答案 0 :(得分:1)
试试这个:
Private Sub UserForm_Click()
Dim whitecard() As String
' create an array from your strings (split using the comma (,))
whitecard = Split("red,blue,green,yellow,purple", ",")
' Randomize the random number generator using the timer,
' otherwise you get the same random numbers
Randomize Timer
Dim low As Long
low = 0 ' the lower bound of your array
Dim hi As Long
hi = 4 ' the upper bound of your array
Dim random As Long
' this is the typical way to use the `Rnd` function to get a specific range of numbers
random = ((hi - low + 1) * Rnd()) +
MsgBox whitecard(random)
End Sub