Excel VBA 简单加减运算代码示例
下面是一个简单的 Excel VBA 代码,实现了加法和减法功能:
Sub add_subtract()
Dim num1 As Integer
Dim num2 As Integer
Dim result As Integer
Dim operator As String
num1 = InputBox('请输入第一个数字:')
operator = InputBox('请输入操作符号(+ 或 -):')
num2 = InputBox('请输入第二个数字:')
If operator = '+' Then
result = num1 + num2
ElseIf operator = '-' Then
result = num1 - num2
Else
MsgBox '操作符号错误,请重新输入'
Exit Sub
End If
MsgBox '计算结果为:' & result
End Sub
以上代码实现了三个变量的声明:num1,num2 和 result,分别用于存储两个数字和计算结果。另外还声明了一个字符串变量 operator,用于存储操作符号。
代码通过 InputBox 函数获取用户输入的数字和操作符号,并根据操作符号执行加法或减法运算。最后通过 MsgBox 函数输出计算结果。
原文地址: https://www.cveoy.top/t/topic/m1hM 著作权归作者所有。请勿转载和采集!