QP School

Full Version: Calculate the average of three numbers in VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub CalculateAverage()
    Dim num1 As Double
    Dim num2 As Double
    Dim num3 As Double
    Dim average As Double

    num1 = 5
    num2 = 3
    num3 = 7

    average = (num1 + num2 + num3) / 3

    MsgBox "The average of " & num1 & ", " & num2 & ", and " & num3 & " is " & average & "."
End Sub

Explanation:

This code defines a subroutine named CalculateAverage. 
It declares five variables num1, num2, num3, and average to hold the values of three numbers and their average. 
It sets the values of num1, num2, and num3 to 5, 3, and 7, respectively. 
The average of the three numbers is calculated and displayed in a message box.

The average of three numbers is calculated by adding them together and then dividing the sum by the count of numbers (in this case, 3). 
The MsgBox function displays the result in a message box.