QP School

Full Version: Additions with variables in VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Description:

This code calculates the sum of two numbers and displays the result in a message box.

Example:

Sub SumNumbers()
    Dim num1 As Double
    Dim num2 As Double
    Dim sum As Double

    num1 = 5
    num2 = 3
    sum = num1 + num2

    MsgBox "The sum of " & num1 & " and " & num2 & " is " & sum & "."
End Sub

Explanation:

This code defines a subroutine named SumNumbers. 
It declares three variables num1, num2, and sum to hold the values of two numbers and their sum. 
It then sets the values of num1 and num2 to 5 and 3, respectively. 
The sum of the two numbers is calculated and displayed in a message box.

The Dim statement is used to declare variables. 
In this case, we use the Double data type to store numbers with decimal places. 
The & operator is used for string concatenation, combining text and variable values in the message box.