0
我想在不使用输入框的情况下创建猜谜游戏。我想让代码读取列A上的猜测,然后告诉我C列是对还是错。宏是用于数字1 - 10,宏应该经过每个猜测。将输入框数据输入转换为单元格数据输入
这里是我的代码 -
Sub VBAGuessingGame()
Dim Secret As Integer
Dim Guess As Integer
Dim Tries As Integer
Randomize 'Initializes random-number generator
Secret = Int((10 * Rnd) + 1) 'Generates a random number between 1 and 10
Guess = 0
Tries = 0
Do While Guess <> Secret
Guess = InputBox("Guess a number between 1 and 10.")
If Guess = Secret Then
Tries = Tries + 1
MsgBox ("You guessed the number!")
MsgBox ("It took you " & Tries & " to guess the number")
ElseIf Guess > Secret Then
Tries = Tries + 1
MsgBox ("Wrong. Too high. Try again.")
Else
Tries = Tries + 1
MsgBox ("Wrong. Too low. Try again.")
End If
Loop
End Sub
您将需要使用公共变量和worksheet_change事件。 –