Вы находитесь на странице: 1из 16

VBA – Basics

Financial Analysis with Modelling in


Excel
XIM Bhubaneswar, 2018-2020
Instructor: Rajiv Bhutani
Financial Modelling using Excel by Rajiv
Bhutani
Variables
• Variable names can consist of alphabets or numbers.
• Variable name must start with an alphabet and not a
number.
• Maximum length allowed for a variable name is 255
characters.
• Variable name cannot be the same as any one of Excel's key
words.  E.g. Invalid variable name: Sheet, Worksheet etc.
• Variable name must consist of one continuous string of
characters.  Words can be separated by either capitalizing
the first letter of each word, or by using the underscore.

Financial Modelling using Excel by Rajiv


Bhutani
Data Types: Numeric
Data Type Name Type Data Range and Remarks

Byte Numeric Whole number between 0 and 255.

Integer Numeric Whole number between -32,768 and 32,767.

Long Numeric Whole number between – 2,147,483,648 and


2,147,483,647.

Currency Numeric Fixed decimal number between -


922,337,203,685,477.5808 and
922,337,203’685,477.5807.

Single Numeric Floating decimal number between -3.402823E38 and


3.402823E38.

Double Numeric Floating decimal number between -


1.79769313486232E308 and 1.79769313486232E308.

Financial Modelling using Excel by Rajiv


Bhutani
Data Types: Non-Numeric
Data Type Name Type Data Range and Remarks

String Text Text.

Date Date Date and time.

Boolean Boolean True or False.

Object Object Microsoft Object.

Variant Any type Default type if the variable is not declared with
any data type. It will accept any kind of data.

Financial Modelling using Excel by Rajiv


Bhutani
Examples
• Sub sumValues()

• 'Variable Declaration
• Dim int_val_1 As Integer
• Dim int_val_2 As Integer
• Dim int_sum As Integer

• 'Initiating the Values


• int_val_1 = 500
• int_val_2 = 1000

• 'Calculating the Total


• int_sum = int_val_1 + int_val_2

• 'Showing the result in the message box


• MsgBox int_sum

• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Examples
• Sub sbStringExample()

• 'Variable Declaration
• Dim strUserName As String

• 'Accepting the data from the user
• strUserName = InputBox("Enter Your Name")

• 'Showing it again in the message box


• MsgBox "Hello!" & strUserName

• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Arrays – One dimensional
• An array is a group of variables with the same data type and name. One can refer
to a specific element by using the array name and the index number.
• Place a command button on worksheet and add the following code lines:

• Private Sub CommandButton1_Click( )


• Dim Names(1 To 5) As String
Names(1) = “Alok"
Names(2) = “Aman"
Names(3) = “Priya"
Names(4) = “Sheetal"
Names(5) = “Tony“
MsgBox Names (2)
MsgBox Names (5)
• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Arrays – One dimensional
• Private Sub CommandButton1_Click( )
• Dim StudentName(1 to 5) As String
For i = 1 To 5
StudentName(i) = InputBox("Enter student
Name")
Cells(i, 1) = StudentName(i)
Next
• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Arrays – One dimensional
• Private Sub CommandButton1_Click( )
• Dim StudentName(3) As String, StudentID(3) As String,
StudentMark(3) As Single
For i = 1 To 3
StudentName(i) = InputBox("Enter student Name")
StudentID(i) = InputBox("Enter student ID")
StudentMark(i) = InputBox("Enter student Mark")
Cells(i, 1) = StudentName(i)
Cells(i, 2) = StudentID(i)
Cells(i, 3) = StudentMark(i)
Next
• End Sub
Financial Modelling using Excel by Rajiv
Bhutani
Arrays – Two dimensional
• Private Sub CommandButton1_Click( )
• Dim Names(1 To 5, 1 To 2) As String
Dim i As Integer, j As Integer
For i = 1 To 5
    For j = 1 To 2
        Names(i, j) = Cells(i, j).Value
    Next j
Next i
MsgBox Names(4, 2)
• End Sub
Financial Modelling using Excel by Rajiv
Bhutani
Custom Data Types
• Option Explicit

• '***** User defined type


• Public Type MyType
• MyInt As Integer
• MyString As String
• MyDoubleArr(2) As Double
• End Type

• '***** Testing MyType as single variable


• Public Sub MyFirstSub()
• Dim MyVar As MyType

• MyVar.MyInt = 2
• MyVar.MyString = "cool"
• MyVar.MyDoubleArr(0) = 1
• MyVar.MyDoubleArr(1) = 2
• MyVar.MyDoubleArr(2) = 3

• Debug.Print "MyVar: " & MyVar.MyInt & " " & MyVar.MyString & " " & MyVar.MyDoubleArr(0) & " " & MyVar.MyDoubleArr(1) & " " &
MyVar.MyDoubleArr(2)
• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Custom Data Types
• Option Explicit

• '***** User defined type


• Public Type MyType
• MyInt As Integer
• MyString As String
• MyDoubleArr(2) As Double
• End Type

• '***** Testing MyType as single variable


• Public Sub MyFirstSub()
• Dim MyVar As MyType

• MyVar.MyInt = 2
• MyVar.MyString = "cool"
• MyVar.MyDoubleArr(0) = 1
• MyVar.MyDoubleArr(1) = 2
• MyVar.MyDoubleArr(2) = 3

• Debug.Print "MyVar: " & MyVar.MyInt & " " & MyVar.MyString & " " & MyVar.MyDoubleArr(0) & " " & MyVar.MyDoubleArr(1) & " " &
MyVar.MyDoubleArr(2)
• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Custom Data Types
• '***** Testing MyType as an array
• Public Sub MySecondSub()
• Dim MyArr(2) As MyType
• Dim i As Integer

• MyArr(0).MyInt = 31
• MyArr(0).MyString = "VBA"
• MyArr(0).MyDoubleArr(0) = 1
• MyArr(0).MyDoubleArr(1) = 2
• MyArr(0).MyDoubleArr(2) = 3
• MyArr(1).MyInt = 32
• MyArr(1).MyString = "is"
• MyArr(1).MyDoubleArr(0) = 11
• MyArr(1).MyDoubleArr(1) = 22
• MyArr(1).MyDoubleArr(2) = 33
• MyArr(2).MyInt = 33
• MyArr(2).MyString = "cool"
• MyArr(2).MyDoubleArr(0) = 111
• MyArr(2).MyDoubleArr(1) = 222
• MyArr(2).MyDoubleArr(2) = 333

• For i = LBound(MyArr) To UBound(MyArr)


• Debug.Print "MyArr: " & MyArr(i).MyString & " " & MyArr(i).MyInt & " " & MyArr(i).MyDoubleArr(0) & " " & MyArr(i).MyDoubleArr(1) & " " &
MyArr(i).MyDoubleArr(2)
• Next
• End Sub

Financial Modelling using Excel by Rajiv


Bhutani
Public Vs Private Declaration

Private and Public are mostly used to
either declare the scope of a variable or a
subroutine (sub).

“Dim” is also used to declare a variable

Dim can be thought of another way of
stating Private
Private Declaration

Private Sub sets the scope so that subs in outside modules cannot call that
particular subroutine.  This means that a sub in Module 1 could not use the Call
method to initiate a Private Sub in Module 2.

Note: If you start at the Application level, you can use Run to override this rule
and access a Private Sub

Private variable means that the variable cannot be accessed or used by
subroutines in other modules. These variables must be declared outside of a
subroutine (usually at the very top of a module).

This type of variable is useful when one subroutine generates a value and that
value is passed to another subroutine in the same module.

Dim variable is used to state the scope inside of a subroutine.  Dim can be
used either inside a subroutine or outside a subroutine (using it outside a
subroutine would be the same as using Private).
Public Declaration

Public Sub means that the subroutine can be called or
triggered by other subs in different modules

Public is the default scope for all subs so we do not need
to add it before the word “sub”

Public variable means that the variable can be accessed or
used by subroutines in outside modules. These variables
must be declared outside of a subroutine (usually at the
very top of a module). This type of variable is useful when
one subroutine generates a value and it needs to be
passed to another subroutine stored in a separate module.

Оценить