Building the project in VBA
What follows is VBA code for a simple calculator that allows the user to enter two numbers and an operator and then perform the corresponding arithmetic operation on the two numbers.
The code initializes some variables and then enters a Do-While
loop allows the user to perform multiple calculations until they choose to quit. Within the loop, the code displays input boxes for the user to enter the first, second, and operator numbers.
After the user enters the values, the code uses a Select Case
statement to check which operator was entered and perform the corresponding calculation. If the user enters an invalid operator or attempts to divide by zero, the code displays a message box with an error message. The result is displayed in a message box if the calculation is successful.
The loop continues until the user enters X
to quit, at which point the sentinel
variable is set to True
, and the loop ends:
Option Explicit Sub VBACalc() Dim n1, n2, res...