Option Explicit Sub お金を入れたときの処理() Dim money_in As Integer Dim price1 As Integer Dim price2 As Integer '投入金額を取得する。 money_in = Worksheets("自動販売機").Cells(13, 7).Value '商品の値段を取得する。 price1 = Worksheets("自動販売機").Cells(5, 3).Value price2 = Worksheets("自動販売機").Cells(5, 4).Value '商品1の購入可能の判断 可能ラン点灯の判断 If money_in >= price1 Then '購入可能(セルを黄色にする) Worksheets("自動販売機").Cells(3, 3).Interior.Color = RGB(255, 255, 0) Else '購入不可(セルを白色にする) Worksheets("自動販売機").Cells(3, 3).Interior.Color = RGB(255, 255, 255) End If '商品2の購入可能の判断 可能ラン点灯の判断 If money_in >= price2 Then '購入可能(セルを黄色にする) Worksheets("自動販売機").Cells(3, 4).Interior.Color = RGB(255, 255, 0) Else '購入不可(セルを白色にする) Worksheets("自動販売機").Cells(3, 4).Interior.Color = RGB(255, 255, 255) End If End Sub Sub 商品1の購入処理() Dim money_in As Integer Dim money_out As Integer Dim price As Integer With Worksheets("自動販売機") If .Cells(3, 3).Interior.Color = RGB(255, 255, 0) Then '購入可能 .Cells(20, 5).Value = .Cells(3, 3).Value 'おつりの計算 money_in = .Cells(13, 7).Value price = .Cells(5, 3).Value money_out = money_in - price 'おつりの表示 If money_out > 0 Then .Cells(17, 7).Value = money_out End If '商品のランプを消す .Cells(3, 3).Interior.Color = RGB(255, 255, 255) .Cells(3, 4).Interior.Color = RGB(255, 255, 255) '投入金額を消す .Cells(13, 7).Value = "" Else '購入不可 End If End With End Sub Sub 商品2の購入処理() Dim money_in As Integer Dim money_out As Integer Dim price As Integer With Worksheets("自動販売機") If .Cells(3, 4).Interior.Color = RGB(255, 255, 0) Then '購入可能 .Cells(20, 5).Value = .Cells(3, 4).Value 'おつりの計算 money_in = .Cells(13, 7).Value price = .Cells(5, 4).Value money_out = money_in - price 'おつりの表示 If money_out > 0 Then .Cells(17, 7).Value = money_out End If '商品のランプを消す .Cells(3, 3).Interior.Color = RGB(255, 255, 255) .Cells(3, 4).Interior.Color = RGB(255, 255, 255) '投入金額を消す .Cells(13, 7).Value = "" Else '購入不可 End If End With End Sub