VBA计算器的全部实现


我们将加入 除零逻辑 和 对话框提示。

后面 就很简单了~~~

提供效果图跟 代码

代码示例

Sub 矩形1_Click()
'
' 矩形1_Click Macro
' 代码编辑

    Dim number1 As Integer
    Dim number2 As Integer
    


    number1 = Cells(5, 4)
    number2 = Cells(6, 4)
    
    Cells(7, 4) = number1 + number2


    
End Sub
Sub 矩形2_Click()
'
' 矩形2_Click Macro
'
    Dim number1 As Integer
    Dim number2 As Integer
    
    number1 = Cells(5, 4)
    number2 = Cells(6, 4)
    
    Cells(7, 4) = number1 - number2
    
End Sub
Sub 矩形4_Click()
'
' 矩形4_Click Macro
'
    Dim number1 As Integer
    Dim number2 As Integer
    
    number1 = Cells(5, 4)
    number2 = Cells(6, 4)
    
    Cells(7, 4) = number1 * number2
End Sub
Sub 矩形5_Click()
'
' 矩形5_Click Macro
'
    number1 = Cells(5, 4)
    number2 = Cells(6, 4)
    
    If number2 = 0 Then
        MsgBox ("除数不能为零~")
    Else
    
        Cells(7, 4) = number1 / number2
    End If
    
End Sub

参考文献:

https://www.yiibai.com/vba/vba_message_box.html