Mathematical functions are essential tools in Excel VBA 365 that allow you to perform complex calculations and numerical operations. These built-in functions save time and improve the accuracy of your calculations. In this lesson, we'll explore the most commonly used mathematical functions with practical examples.
The Rnd function generates random numbers between 0 and 1. To generate random numbers in a specific range, use the formula: Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
.
Both Int and Fix remove the decimal part of a number, but they handle negative numbers differently:
Int(-6.34) = -7
(returns the lower integer)Fix(-6.34) = -6
(simply truncates the decimal)The Round function follows "banker's rounding" (rounds to nearest even number when exactly halfway between two numbers). For example:
Round(2.5) = 2
Round(3.5) = 4
This example demonstrates how to generate 10 random numbers between 0 and 1:
Private Sub CommandButton1_Click() For x = 1 To 10 MsgBox Rnd Next x End Sub
Simulate a dice roll (numbers 1-6) using Rnd and Int functions:
Private Sub CommandButton1_Click() Dim diceRoll As Integer diceRoll = Int(Rnd * 6) + 1 MsgBox "You rolled: " & diceRoll End Sub
This example generates a random 8-character password using Rnd and Chr functions:
Private Sub GeneratePassword() Dim password As String Dim i As Integer Randomize ' Initialize random number generator password = "" For i = 1 To 8 ' Generate random character between ASCII 33 (!) and 122 (z) password = password & Chr(Int((122 - 33 + 1) * Rnd + 33) Next i MsgBox "Your new password: " & password End Sub
Calculate and display square roots from 1 to 10:
Private Sub CommandButton1_Click() For i = 1 To 10 MsgBox "Square root of " & i & " is " & Sqr(i) Next End Sub
This advanced example uses multiple math functions to calculate monthly payments:
Private Sub CalculateMortgage() Dim principal As Double, rate As Double, years As Integer Dim monthlyPayment As Double, monthlyRate As Double principal = 200000 ' Loan amount rate = 0.05 ' 5% annual interest years = 30 ' Loan term monthlyRate = rate / 12 monthlyPayment = principal * monthlyRate / (1 - (1 + monthlyRate) ^ (-years * 12)) MsgBox "Monthly payment: $" & Round(monthlyPayment, 2) End Sub
This example demonstrates multiple functions in action, creating a table of values:
Private Sub CommandButton1_Click() Dim n As Integer Dim x As Single ' Clear previous data Range("A1:F20").ClearContents ' Add headers Cells(1, 1) = "Original" Cells(1, 2) = "Int(x)" Cells(1, 3) = "Fix(x)" Cells(1, 4) = "Round(x,4)" Cells(1, 5) = "Log(x)" Cells(1, 6) = "Sqr(x)" ' Generate and calculate values n = 2 Do While n < 12 x = Rnd * 7 Cells(n, 1) = x Cells(n, 2) = Int(x) Cells(n, 3) = Fix(x) Cells(n, 4) = Round(Cells(n, 1), 4) Cells(n, 5) = Round(Log(x), 4) Cells(n, 6) = Round(Sqr(x), 4) n = n + 1 Loop End Sub
Rnd
function generates random numbers - remember to use Randomize
Int
and Fix
differ in how they handle negative numbersRound
uses banker's rounding which may differ from standard roundingWorksheetFunction
Copyright ® 2020 Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page