0% found this document useful (0 votes)
39 views

Introduction To Excel VBA Programming - SARA E

The document introduces Excel VBA programming and provides examples of creating macros using the macro recorder and VBA scripting. It covers basic VBA concepts like constants, variables, loops, conditions, functions and user forms with examples.

Uploaded by

Mike Pyanx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Introduction To Excel VBA Programming - SARA E

The document introduces Excel VBA programming and provides examples of creating macros using the macro recorder and VBA scripting. It covers basic VBA concepts like constants, variables, loops, conditions, functions and user forms with examples.

Uploaded by

Mike Pyanx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INTRODUCTION

TO
EXCEL
VBA
PROGRAMMING
1. INTRODUCTION
In today’s world, excel is used everywhere from small shops to big MNCs. It is a
database tool and can do lot of things to assist our work in many ways. Most of the times, excel is
used to perform some task repetitively which might consume more time. To overcome this vba
macros can be used to perform these task automatically and without manual error.
We will learn about how to create simple vba programs in excel in this tutorial
First step to create vba macros in excel is to enable the developer tool bar.
Goto File->Options->Customize Ribbon
Check the Developer check box and click ok


Once checkbox is enabled, Developer tab will appear in the menu bar.
Now we can start creating our own VBA programs.

2. MACRO RECORDING AND RUNNING:


Macros can be created using 2 ways.
By Recording
By vba Scripting on own
Recorded Macros:
This is the simplest way to create excel macros. This is useful when the same task has to be
done regularly. It is similar like voice recording and playing it back.
How to Record a macro:
Let us consider, we are calculating a sum for series of numbers every day.
Before doing this task, either click the record button at the view tab of the main menu

Or Press the record button in the leftmost bottom of the sheet

After performing the task, stop the recording .


We can give any name to the macro or it will take the default name like “Macro 1”.
Next time when you want to run the same task, you don’t want to do the whole process once again.
At the click of the button, result will be executed.
Macro Running:
To run the macro, Goto View->Macros-View Macros, below screen will appear. Click Run

3. VBA BASICS
Recording macros are the easiest way, but it cannot be used all the time as the task we are
doing might not be exactly the same every time. So to adapt the macro to all conditions, we might
need to write coding on our own or can modify the recorded macro coding to suit it to the criteria.
To go to Coding window, press Alt+F11 and to create a new macro or goto Insert->Module
Below is the coding window and program syntax is shown.

4. VBA PROGRAMMING
4.1 Msgbox:
Msgbox is used to display messages on the screen .Now we will start with simple program.
Let us display “Good Morning” on excel screen.
Syntax:
Msgbox(“YOUR MESSAGE”)
Example 1:
Sub Macro()
Msgbox(“Good Morning”)
End Sub
To run a macro, press F5 or goto View->Macros->Macro Name->Run
To run line by line, press F8 in coding window.


Output:
Below is the output of the above program

4.2 Constant & Variables


Data type used in this project will be similar to other programming languages like integer,
long, double, string etc. ”Dim” is the keyword which is used to describe the datatype.
Eg:
Dim i as Integer
Dim name as String

Variables are the one whose value varies during the course of the program and constant
have a fixed value throughout the program. Example for constant is any number or fixed word.
Example 2:
In our second example let us display a name from the excel cell B1.
Sub Macro()
Dim name as string
name=Range(“B1”)
Msgbox(“My Name is ”&name)
End Sub
Here “name” is a variable and it takes a value from the cell B1.
Output:

4.3 Important functions used in Excel VBA


 Workbook Functions:
Workbooks.Open(“C:\Filename.xlsx”) [To open workbook]
Workbooks("C:\Filename.xlsx ").Close [To close workbook]
ActiveWorkbook.SaveAs Filename:="C:\ Filename.xlsx"

 Worksheet Functions:
Sheets("Sheet1").Activate [To activate a worksheet by its name]
Sheets("Sheet1").Cells.Select[To select all the cells in the worksheet]
Sheets("Sheet1").Cells.ClearContents [To delete the values in all cells of the
entire sheet]
To copy cells of 1 sheet and paste it in other sheet.
Sheets("Sheet1").Cells.Copy
Sheets("Sheet2").Activate
Range("A1").Select
ActiveSheet.Paste
Rows(1).Copy/Select [To copy/select the cells of row 1]
Columns(1).Copy/Select [To copy/select the cells of column 1]
 Cell Functions:
To Select a cell
Cell(row_number,column_number).Select
Range(“A1”).Select [To select cell with row 1 and column 1]

4.4 For Loop


Syntax:
For VARIABLE=start value to VARIABLE=end value
…..
Next
Eg:
For i=0 to 5
Next
[This will execute the loop 5 times each time incrementing the value of i by 1]

4.5 If Else Condition


Syntax:
If CONDITION then
…..
Else
…….
Endif
Example 3:
VBA program to check whether marks obtained by student is
Pass(Marks>=35)/Fail(Marks<35)
Sub macro()
For i = 2 To 6
If Cells(i, 2) >= 35 Then
Cells(i, 3) = "Pass"
Else
Cells(i, 3) = "Fail"
End If
Next
End Sub

Example 4:
For above program,If Elseif function could be used to put grade for the marks
Grade-A[If marks>=80]
Grade-B[If marks 60 to 79]
Grade-C[If marks 35 to 59]
Grade-D[If marks < 35]
Sub macro()
For i = 2 To 6
If Cells(i, 2) < 35 Then
Cells(i, 3) = "Grade D"
ElseIf Cells(i, 2) >= 35 And Cells(i, 2) < 59 Then
Cells(i, 3) = "Grade C"
ElseIf Cells(i, 2) >= 60 And Cells(i, 2) < 80 Then
Cells(i, 3) = "Grade B"
ElseIf Cells(i, 2) >= 80 And Cells(i, 2) <= 100 Then
Cells(i, 3) = "Grade A"
End If
Next
End Sub

4.6 While Condition


Syntax:
While CONDITION
Wend
Example 5:
To Print numbers from 1 to 10 in column 1
Sub macro()
i = 1
While i < 11
Cells(i, 1) = i
i = i + 1
Wend
End Sub

4.6 Do Loop
Syntax:
Do
Loop
Example 6:
To Print numbers from 1 to 10 in column 1 using Do Loop
Sub macro()
i = 1
Do
Cells(i, 1) = i
i = i + 1
Loop While i <= 10
End Sub

5. USER FORM CONTROLS:


To insert userform, Goto Insert->Userform

User form is GUI window which is similar like a website.

5.1 Command Button:


It is a button, When we click this button corresponding coding will be executed.
5.2 Label:
It is used to give some label to certain field

5.3 Text Box


By using this, we can get input from the user

Example 7:
To Get name using Text box and print msgbox using it

Create user form as above and when we run this,below window appears.
Below Coding can be written to display the name.
Private Sub CommandButton1_Click()
MsgBox ("My Name is" & TextBox1.Text)
End Sub

5.4 Check Box


Check box is used to select multiple items
5.5 Option Button
Option button can select only 1 item among the given options

5.6 Combo Box


Combo box can be used to select one item from the given drop down items

5.7 List Box


List box can be used to list data which have more than 1 line.

Example 8:
In the next example, we can try to employ the above functions. To get input data from a student
and store it in excel sheet.


Private Sub UserForm_Initialize()
ComboBox1.AddItem ("BE")
ComboBox1.AddItem ("BSC")
ComboBox1.AddItem ("BCA")
ComboBox1.AddItem ("MCA")
ComboBox1.AddItem ("MBA")
End Sub
Private Sub CommandButton1_Click()
i = Cells(Rows.Count, "A").End(xlUp).Row '[To Count no of values in Column 1]'
Cells(i + 1, 1) = TextBox1.Text
If OptionButton1.Value = True Then
Cells(i + 1, 2) = "Male"
Else
Cells(i + 1, 2) = "Female"
End If
Cells(i + 1, 3) = ComboBox1.Text
Count = 0
If CheckBox1.Value = True And CheckBox2.Value = True And CheckBox3.Value = True
Then
Check = CheckBox1.Caption & ", " & CheckBox2.Caption & ", " & CheckBox3.Caption
ElseIf CheckBox1.Value = True And CheckBox2.Value = True Then
Check = CheckBox1.Caption & ", " & CheckBox2.Caption
ElseIf CheckBox1.Value = True And CheckBox3.Value = True Then
Check = CheckBox1.Caption & ", " & CheckBox3.Caption
ElseIf CheckBox2.Value = True And CheckBox3.Value = True Then
Check = CheckBox2.Caption & ", " & CheckBox3.Caption
ElseIf CheckBox1.Value = True Then
Check = CheckBox1.Caption
ElseIf CheckBox2.Value = True Then
Check = CheckBox2.Caption
ElseIf CheckBox3.Value = True Then
Check = CheckBox3.Caption
End If
Cells(i + 1, 4) = Check
End Sub

By this we have discussed the basic things which is needed to prepare an excel VBA macro.
Beginners can start with creation of simple macros, debug for errors and run them. If we have the
interest and determination , then we can learn VBA macro in just few days.

ALL THE BEST…..

You might also like