Introduction To Excel VBA Programming - SARA E
Introduction To Excel VBA Programming - SARA E
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.
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
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:
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 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
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
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…..