Microsoft Excel VBA Functions: Introduction to Procedures (2024)

Calling a Sub Procedure

Once you have a procedure, whether you created it or it is part of the Visual Basic language, you can use it. Using a procedure is also referred to as calling it.

Before calling a procedure, you should first locate the section of code in which you want to use it. To call a simple procedure, type its name. Here is an example:

Sub CreateCustomer() Dim strFullName As String strFullName = "Paul Bertrand Yamaguchi"End SubSub Exercise() CreateCustomerEnd Sub

Besides using the name of a procedure to call it, you can also precede it with the Call keyword. Here is an example:

Sub CreateCustomer() Dim strFullName As String strFullName = "Paul Bertrand Yamaguchi"End SubSub Exercise() Call CreateCustomerEnd Sub

When calling a procedure, without or without the Call keyword, you can optionally type an opening and a closing parentheses on the right side of its name. Here is an example:

Sub CreateCustomer() Dim strFullName As String strFullName = "Paul Bertrand Yamaguchi"End SubSub Exercise() CreateCustomer()End Sub

Procedures and Access Levels

Like a variable access, the access to a procedure can be controlled by an access level. A procedure can be made private or public. To specify the access level of a procedure, precede it with the Private or the Public keyword. Here is an example:

Private Sub CreateCustomer() Dim strFullName As String strFullName = "Paul Bertrand Yamaguchi"End Sub

The rules that were applied to global variables are the same:

  • Private: If a procedure is made private, it can be called by other procedures of the same module. Procedures of outside modules cannot access such a procedure.
    Also, when a procedure is private, its name does not appear in the Macros dialog box
  • Public: A procedure created as public can be called by procedures of the same module and by procedures of other modules.
    Also, if a procedure was created as public, when you access the Macros dialog box, its name appears and you can run it from there

Microsoft Excel VBA Functions: Introduction to Procedures (2024)

FAQs

What are procedures and functions in VBA? ›

You run a VBA procedure to perform an automated task in Excel like building a PivotTable. Sub and Function procedures are used to automate Excel and also perform calculations. There is also a Property procedure that is used in the creation of objects.

What is the difference between procedure and function in VBA? ›

A VBA function is similar to a sub procedure, only that the former can return a value whereas the latter cannot.

What are the three types of VBA procedures you can create? ›

You can create a Sub, Function, or Property procedure. Type Sub, Function, or Property.

What is the difference between a module and a procedure in Excel VBA? ›

A procedure is a unit of code enclosed either between the Suband End Sub statements or between the Function and End Function statements. A procedure should accomplish a simple well-defined task. Modules are workbook sheets that contain code. Each module can contain a declarations section followed by procedures.

What is the difference between procedure and function in Excel? ›

A function has a return type and returns a value. A procedure does not have a return type. But it returns values using the OUT parameters.

What are the two main types of procedures one can code within VBA they are? ›

There are two types of procedures in VBA: sub procedures form an action in Excel and begin with the text "Sub", and function procedures that carry out calculations and return a value.

Why use a function instead of a procedure? ›

Procedures can not be utilized in a SELECT statement, whereas Functions can be embedded in a SELECT statement. Stored Procedures cannot be used in SQL statements anywhere in a WHERE (or a HAVING or a SELECT ) block, whereas Functions can. Functions that return tables can be treated as another Rowset.

Why do we use procedure instead of function? ›

Stored procedures take no parameters, can modify database objects, and need not return results. Functions can only have input parameters, whereas procedures can have either input or output parameters. Functions can be called from procedures, but procedures cannot be called from functions.

How are procedures different from functions? ›

A function is used to calculate result using given inputs. A procedure is used to perform certain task in order. A function can be called by a procedure. A procedure cannot be called by a function.

Is VBA still in demand? ›

While VBA might not be the most modern programming language, Microsoft Office is still extremely popular in the business world. Excel still uses VBA to streamline repetitive tasks and processes. Considering the number of spreadsheets that use VBA, it's far from obsolete.

Is it hard to learn VBA? ›

Learning VBA can be challenging at first, especially when dealing with scripts and handling the Microsoft Scripting Reference library. However, it can be more manageable with the support of a structured course. VBA is often compared to Python for automation tasks.

What is the difference between VBA and script in Excel? ›

Both frameworks are designed to empower people who may not consider themselves programmers to create small programs in Excel. The fundamental difference is that VBA macros are developed for desktop solutions and Office Scripts are designed for secure, cross-platform, cloud-based solutions.

What is the difference between macro and function in VBA? ›

UDF and VBA macro work in different ways. For example, when you create a UDF in the Visual Basic Editor, you start with a statement Function and end with an End Function. When you record a macro, you start with a statement Sub and end with an End Sub. Not all Visual Basic operators can be used to create UDFs.

What is the difference between macro and VBA code in Excel? ›

VBA and macros in Excel are tools that extend the functionality of Microsoft Excel and allow the user to write custom programs or use generated scripts to automate tasks, connect different applications, and tackle problems in a different way. VBA is the programming language used to create and deploy macros.

What does procedure mean in VBA? ›

A procedure is a block of Visual Basic statements enclosed by a declaration statement ( Function , Sub , Operator , Get , Set ) and a matching End declaration. All executable statements in Visual Basic must be within some procedure.

What are procedures and functions in coding? ›

In programming a procedure or function is a defined piece of code that can be run multiple times. Similar to functions in mathematics, in programming functions have to have an input and an output, whilst a procedure can just run code without having any input or output.

What is the meaning of function and procedure? ›

A procedure is a routine that can accept arguments but does not return any values. A function is a routine that can accept arguments and returns one or more values. User-defined routine (UDR) is a generic term that includes both user-defined procedures and user-defined functions.

What is the purpose of procedures and functions? ›

Functions and procedures are the basic building blocks of programs. They are small sections of code that are used to perform a particular task, and they are used for two main reasons. The first reason is that they can be used to avoid repetition of commands within the program.

Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6301

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.