Calculate the average of three numbers in VBA - Printable Version +- QP School (https://qomplainerzschool.lima-city.de) +-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3) +--- Forum: Excel VBA Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=48) +--- Thread: Calculate the average of three numbers in VBA (/showthread.php?tid=5238) |
Calculate the average of three numbers in VBA - Qomplainerz - 07-27-2023 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. |