excel - Convert inputbox data entry to cell data entry -
i want create guessing game without using input boxes. want code read guesses on column , tell me if right or wrong in column c. macro numbers 1 - 10 , macro should go through each guess.
here's code -
sub vbaguessinggame() dim secret integer dim guess integer dim tries integer randomize 'initializes random-number generator secret = int((10 * rnd) + 1) 'generates random number between 1 , 10 guess = 0 tries = 0 while guess <> secret guess = inputbox("guess number between 1 , 10.") if guess = secret tries = tries + 1 msgbox ("you guessed number!") msgbox ("it took " & tries & " guess number") elseif guess > secret tries = tries + 1 msgbox ("wrong. high. try again.") else tries = tries + 1 msgbox ("wrong. low. try again.") end if loop end sub
put behind sheet1(sheet1), if that's sheet you're using:
private sub worksheet_change(byval target range) dim guess integer, secret integer thiscolumn = target.column thisrow = target.row if isnumeric(target.cells.value) guess = target.cells.value else: guess = 0 end if randomize secret = int(10 * rnd) + 1 if thiscolumn = 1 if guess = secret cells(thisrow, 3).value = "right" else cells(thisrow, 3).value = "wrong" end if cells(thisrow, 4).value = secret end if end sub
Comments
Post a Comment