添加两个文本框值

时间:2018-03-18 17:11:50

标签: excel vba

任何人都可以帮我解决这个vba代码。我试图在这两个文本框中添加值,但收到溢出错误消息。使用Int()函数随机生成值,并将ans声明为整数

addsub.Cells(rowmark, 1).Value = txtbxaddend1.Text
addsub.Cells(rowmark, 2).Value = "+"
addsub.Cells(rowmark, 3).Value = txtbxaddend2.Text
addsub.Cells(rowmark, 4).Value = "="
addsub.Cells(rowmark, 5).Value = Format(txtbxsum.Text, "#,#00,000")

**ans = txtbxaddend1 + txtbxaddend2 <----PROBLEM CODE**

If txtbxsum = ans Then
    addsub.Cells(rowmark, 9).Value = "1"
    Else
        addsub.Cells(rowmark, 9).Value = "0"
End If

提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用

ans = CDbl(IIf(txtbxaddend1 = "", 0, txtbxaddend1)) + CDbl(IIf(txtbxaddend2 = "", 0, txtbxaddend2))

确保您不处理空文本文本框并将非空值转换为Double数字