Excel VBAを始めるには(014)

今回はIF-Thenの条件判定式。
数あてGame、乱数で発生させた数値と入力した数値が一致させる。
10回が限度。
入力都度、大きいか、小さいかを答える仕組み。
更にExcelらしく、その値をセルに記入させた。

以下、Source

Sub kazuate01()
'
' kazuate01 Macro
'
'乱数で発生させ数を当てるGame

'
Dim Ran_n, Ipt_n As Integer

Dim i As Integer

Dim Str_c As String



Sheets("Sheet4").Select

'データ表示部分の初期化

Range("A1:C11").Select
Selection.ClearContents


'最初のアナウンス画面

MsgBox ("数あてのゲームです 10回まで")

Ran_n = Int((99 + 1) * Rnd + 1) 'ここで99までの乱数を発生させる



For i = 1 To 10

Str_c = InputBox("1-99までの値を入力してください") '数値入力画面

Ipt_n = Val(Str_c) '入力は文字型のため、数値に変換


If Ipt_n > Ran_n Then 'ここからが判定式入力数値と比較する

MsgBox ("Too Big!!") '結果をセルに書き込む
Cells(i, 1) = Ipt_n
Cells(i, 2) = "Too Big"

End If

If Ipt_n < Ran_n Then

MsgBox ("Too Small!!")
Cells(i, 1) = Ipt_n
Cells(i, 2) = "Too Small"


End If

If Ipt_n = Ran_n Then

MsgBox ("OK!!")

Cells(i, 1) = Ipt_n
Cells(i, 2) = "OK!!"

i = 10 '完了したらLoopから脱出

End If

Next i



End Sub



わかお かずまさ
VegaSystems

📷📷📷📷📷📷
#LAN_PRO
#Bloguru
#kuma

People Who Wowed This Post

×
  • If you are a bloguru member, please login.
    Login
  • If you are not a bloguru member, you may request a free account here:
    Request Account
This user only allows bloguru members to make comments.
If you are a bloguru member, please login.
Login
If you are not a bloguru member, you may request a free account here:
Request Account