Open the VBE (+) and create a module (as described in).
To understand how a VBA code works, copy the text below in the Code Window while the newly created module is selected in the Project Explorer ("Module1").
Sub HelloVBAWorld() MsgBox "Hello VBA World" End Sub
Your Code Window will be like this:
With this we have a macro called HelloVBAWorld which, when executed, sends the message "Hello VBA World" to the user.
When running, the following text box should appear:
The MsgBox feature is widely used to leave warnings to users, whether running a routine, for example, or running a .
The abbreviation Sub stands for Subroutine.
Sub indicates to the VBA where the code starts while End Sub indicates where it ends.
Sometimes we will omit the Sub start and end declaration to highlight content:
Omiting the Sub declaration
MsgBox "This is an example"
Showing the Sub declarationThe code should always be contained within a Sub and End Sub to run (unless it is contained between a Function and End Function for example, but we will see this later).
Sub Example() MsgBox "This is an example" End Sub
All Sub needs to be followed by a name (in our example above, the name is "HelloVBAWorld").
The name must comply with the following rules:
At the end of the name, there must be parenthesis"()".
Type Sub, the name of the macro and press the Enter key, the VBA will automatically add the "()" and the End Sub.
SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.
Excel ® is a registered trademark of the Microsoft Corporation.
© 2023 SuperExcelVBA | ABOUT