如果为True,则将VBA添加到另一个值

时间:2016-11-04 14:41:54

标签: excel vba excel-vba if-statement excel-2010

我正在学习VBA,所以我对它很陌生。我正在创建一个骰子游戏,只是练习和修补代码。我试图找到一种方法来增加或减少用户从他拥有的金额中下注的金额。因此,如果他猜测接近掷骰子,他将赢得并赢得他所下注的金额,并且会增加他的余额。我的问题是试图弄清楚如何从余额中增加或减少下注金额。我试图使用一个If语句,说明如果玩家获胜,则添加投注以平衡,如果他输了减去余额下注。在VBA中执行此操作的最佳方法是什么?感谢您提供的任何帮助。

Sub CommandButton1_Click()
Dim bet As Integer
Dim balance As Integer
Dim player As Integer
Dim computer As Integer

bet = Range("B11").Value
balance = Range("A14").Value
player = Range("A19").Value
computer = Range("L19").Value
Range("D14").Value = 5000

Beep

If bet < balance Then Worksheets(1).Calculate
If player < computer Then MsgBox "You Won!"
If player > computer Then MsgBox "You Lost!"
If player = computer Then MsgBox "It's a Tie!"

If player < computer Then Range("D14").Value bet

If player > computer Then

End Sub

Game and Code

编辑:添加代码

2 个答案:

答案 0 :(得分:0)

If player < computer Then Range("D14").Value = Range("D14").Value + bet

If player > computer Then Range("D14").Value = Range("D14").Value - bet

或者您可以将区域更改为此

Beep

If bet < balance Then Worksheets(1).Calculate
If player < computer Then MsgBox "You Won!": Range("D14").Value = Range("D14").Value + bet
If player > computer Then MsgBox "You Lost!": Range("D14").Value = Range("D14").Value - bet
If player = computer Then MsgBox "It's a Tie!"

End Sub

答案 1 :(得分:0)

您可以使用类似

之类的IIF来回复您的要求
if Computer<>Player then Range("D14").Value=Range("D14").Value + iif(Player>Computer,(0-dblStake),Stake‌​)