Sunday, October 4, 2020

Insert a module and basics of programming

In this blog we will discuss how to "insert a module in VBA" and also we will see the basics of programming in VBA using sub procedures and functions.

  VBA| Insert a module & VBA programming basics | Part-3


In this blog we will discuss how to "insert a module in VBA" and also we will see the basics of programming in VBA using sub procedures and functions.

First of all we will discuss the steps to insert a module in VBA which are as follows:-

  1. Open the Excel Application.
  2. Once Excel Application is opened then select blank workbook.
  3. Now the Excel file is opened then press the Alt+F11 keys simultaneously.
  4. A new window will open. In that window click on Insert and then module.
  5. A new Editor window will open where we can write our code.
Now we will discuss about the Sub procedures and functions in VBA.

Sub Procedures - A sub procedure is the block of statements which is enclosed by a particular declaration statement and an End declaration, The main purpose of sub procedures is to carry out a particular task or action. To define a sub procedure use the sub keyword followed by the name.

Syntax to define a sub procedure is as follows:-

sub subname()

Statements to be executed

End Sub

Functions - A function is a VBA code that performs calculations and returns a value. To define a function in VBA we need to use the function keyword followed by the name of the function and the parameters or arguments of the function if any.

Syntax to define a function is as follows:-

function functionname(parameters)

statements to be executed

End function

The main difference between a sub procedure and a function is that sub procedure does not return a value whereas we can return a value through a function.

We can call a function inside a procedure but a procedure cannot be called inside a function.

So the basic syntax of programming in VBA will look like this.

sub SubName()

Dim variable1 As Datatype, variable2,variable3 As Datatype

Statements to be executed

End Sub

So in the above syntax Dim stands for dimension.

Now we will look at the basics of programming in VBA. So as we can see that variable1 and variable3 has their respective datatype defined but we have not defined the datatype for variable2 so what will be it's datatype in such scenario. So variable2 will take variant as the datatype by default.

Suppose for example if we want to add two numbers by using a sub procedure so the code that we will create will look like this as follows:-

Sub AddTwoNumbers()

Dim firstNum As Integer, SecondNum As Integer, result As Integer

firstNum=1
SecondNum=2

result=firstNum+SecondNum

msgbox result

Now we will create a function for adding two numbers and will call that function inside a sub procedure.

function AddTwoNumbers(ByVal vFirstNum As Integer,ByVal vSecondNum As Integer)

Dim vResult As Integer
vResult=vFirstNum+vSecondNum

End Function

Sub Test()

Dim FirstNum As Integer, SecondNum As Integer, result as Integer

FirstNum=1
SecondNum=2

result=AddTwoNumbers(FirstNum,SecondNum)

msgbox result

End Sub

In case of your queries then do please feel free to reach out to me with your queries and I will respond to your queries.

smarty con

Author & Editor

3 comments: