

Note 1: You can use “Option Private Module” to hide subs in the current module.

You can see it is simple how you declare a variable. The next example shows you how to specify the return type Function PerformCalc() As Long End Function This means that VBA will decide the type at runtime. If you don’t add the type then it is a Variant type by default. It is optional to add a return type to a function. You normally create a function when you want to return a value.Ĭreating a function is similar to creating a sub Function PerformCalc() There are other differences which we will look at but this is the main one. The major difference is that a function can return a value – a sub cannot. When you create your Sub you can add some code like the following example shows: Sub WriteValues()Ī Function is very similar to a sub. The end of the Sub is marked by the line End Sub. The name cannot be a reserved word in VBA.The name must be unique in the current workbook.The name must begin with a letter and cannot contain spaces.When you give it a name keep the following in mind: You declare the sub by using the Sub keyword and the name you wish to give the sub. The following is an example of an empty sub Sub WriteValues() That means that VBA carries out their instructions.

When you run a sub, all the lines of code it contains are executed. For the rest of this post I will refer to them as subs.Ī sub/macro is where you place your lines of VBA code. This often leads to confusion so it is a good idea to remember it. In Excel VBA a sub and a macro are essentially the same thing. Let’s start by looking at what is the VBA sub?
