A Subroutine (or Sub procedure) in Excel VBA 365 is a block of code that performs specific tasks when called. Unlike event procedures tied to controls, subroutines are standalone procedures that can be executed from multiple places in your code.
To create a subroutine in Excel VBA 365:
Sub YourSubroutineName(parameters) ' Your code here End Sub
This example demonstrates a simple subroutine that displays a message:
Sub GreetUser() MsgBox "Welcome to Excel VBA 365!", vbInformation, "Greeting" End Sub
This example shows how to pass parameters to a subroutine:
Sub CalculateArea(length As Double, width As Double) Dim area As Double area = length * width MsgBox "The area is: " & area, vbInformation, "Area Calculation" End Sub
You can call subroutines in several ways:
' Method 1: Using Call keyword Call GreetUser ' Method 2: Without Call keyword CalculateArea 10, 5 ' Method 3: From another subroutine Sub MainProcedure() Call GreetUser CalculateArea 15, 20 End Sub
Functions in Excel VBA are similar to subroutines but with one key difference - they return a value. Functions can be either built-in (provided by VBA) or user-defined (created by you).
Excel VBA provides numerous built-in functions. Two commonly used ones are:
result = MsgBox(Prompt, Buttons, Title)
The InputBox function creates a dialog box for user input:
userInput = InputBox(Prompt, Title, DefaultText, XPos, YPos)
Sub GetUserName() Dim userName As String userName = InputBox("Please enter your name:", "User Identification") If userName <> "" Then MsgBox "Hello, " & userName & "!", vbInformation, "Greeting" Else MsgBox "You didn't enter a name.", vbExclamation, "Notice" End If End Sub
User-defined functions (UDFs) are custom functions you create to perform specific calculations or operations. They can be used in VBA code or directly in Excel worksheets.
Public Function FunctionName(parameters) As ReturnType ' Code FunctionName = returnValue End Function
Function CubeRoot(num As Double) As Double If num < 0 Then CubeRoot = -((-num) ^ (1 / 3)) Else CubeRoot = num ^ (1 / 3) End If End Function
Function CalculateHypotenuse(a As Double, b As Double) As Double ' Calculate hypotenuse using Pythagorean theorem CalculateHypotenuse = Sqr(a ^ 2 + b ^ 2) End Function
Function CalculateSalary(basePay As Double, hoursWorked As Double, _ Optional overtimeRate As Double = 1.5) As Double Const normalHours As Double = 40 Dim regularPay As Double, overtimePay As Double If hoursWorked <= normalHours Then CalculateSalary = basePay * hoursWorked Else regularPay = basePay * normalHours overtimePay = (hoursWorked - normalHours) * basePay * overtimeRate CalculateSalary = regularPay + overtimePay End If End Function
Feature | Subroutine | Function |
---|---|---|
Returns value | No | Yes |
Can be used in worksheet | No | Yes |
Calling syntax | Call SubName or SubName | result = FunctionName() |
Declaration | Sub...End Sub | Function...End Function |
Copyright ® 2020 Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page