Learning the basics
Procedures: Sub vs Function
In VBA, programs are usually written using either a Sub procedure or a Function procedure. Both procedures can take arguments by value or by reference. The main difference between a Sub and a Function is that a Sub can not return a value while a Function can (for Subs, this can be bypassed by using parameters passed by reference).
Sub
A Sub has to begin with the statement Sub, and end with the statement End Sub.
Sub ExampleSub() MsgBox "My first Sub procedure!" End Sub
Function
A Function has to begin with the statement Function, and end with the statement End Function. The value returned by a function is the same as the function name.
Function Squared(num As Integer) Squared = num ^ 2 End Function ' We can also make a function that does not return anything. Function myFunc() MsgBox "My second Function procedure!" End Function
Interaction with each other
Both Functions and Subs can be called from within each other. Using such procedures to split your program into smaller parts is a good way to keep the code clean, reuseable and easy to read.
Sub displayMsg() MsgBox "Area" End Sub Function area(num1 As Integer, num2 As Integer) area = num1 * num2 End Function ' Now, let's make a Function and a Sub calling the procedures above. ' Both will do exactly the same. Sub mainSub() displayMsg MsgBox area(3, 17) End Sub Function mainFunction() displayMsg MsgBox area(3, 17) End Function
What is the output of mainSub and mainFunction?
If...Then...Else
The If...Then...Else statement in VBA is similar to If statements found in other languages, such as Pyhton or C++. The main difference is the syntax, which we will shown using some examples. Note that the conditions used in the If...Then...Else statements are further described in the chapter Operators.
Loops
Infinite loops in VBA can be exited by pressing Esc + Break. Occasionally this does not work, and you will have to force Excel to close, e.g. by pressing Ctrl + Shift + Esc and right-click on Excel. Forcing an exit may not save your current work, so make sure to save while writing.
Operators
Explicit vs implicit variable declaration
When writing larger pieces of code, knowing the difference between explicit and implicit variable declaration may save you several hours of debugging.
Useful functions
Format
The Format function is a helpful tool when formatting strings to your preference. Since Microsoft already has made a really in-depth explanation including some examples, we have chosen to only show you some additional examples here.
MsgBox
The MsgBox function displays a message in a dialog box and waits for the user to click a button. You may have seen it used in our other tutorials as a simple way to display the value of a variable, but this is just the function in its simplest use. In addition to a message, you can specify e.g. the title or the buttons. The return value of MsgBox is a value (integer) indicating which button the user clicked. To learn about all the buttons and their return values, read the function documentation.