Namasteđđ My Dear friends.
Hope you all are doing well and
ready for another Macro thing.
Friends, todayâs topic is something
very important and useful function in Macro.
Before we start, I have to discuss a situation
with you.
Suppose u had a fight with your
close friend and you are responsible for the same. Due to fight you donât talk
with each other.
Assuming heâll not come back and
talk to you.
Now you have been left with Two Options:
-
1) Youâll call him/her and say sorry.
OR
2) Let him/her go.
IF you'll choose 1st Option then "youâll preserve your friendship",
OR
OR
ELSE "youâll break the friendship".
Similarly, in Macros also we can put
options and result will come accordingly.
The function to do so will be cover in Todayâs Topic i.e.
IF
AND ELSE.
So, letâs Begin.
Weâll take two examples in this
blog.
First, we will use if else function
for the above situation that we discussed.
As you can see in above image, we have
to type answer as âaâ or âbâ.
So, first weâll write the code for
the same in VBA.
If we type âaâ then response should
be âYouâll Preserve Your Friendshipâ.
If we type âbâ then response should
be â Youâll Break the Friendshipâ.
2) From the tabs given in VBA Editor click
on Insert and Select Module.
3) This will create a Module in which weâll write the codes.
4) All the codes in VBA starts with âSub
âMacro_Nameâ ()â and ends with âEnd Subâ without quotations.
5) Copy or write the following code:-
Sub IF_ELSE()
If Range("B6").Value = a Then
Range("B9").Value = "Youâll
Preserve Your Friendship"
Else
Range("B9").Value = "You'll
Break the Friendship"
End If
End Sub
Learning Points from above Codes: -
§ In above code as you can see it starts
with Sub and ends with End Sub
§ Range(âB6â).value represent the
value of cell âB6â where weâll type our answer.
§ Range(âB9â).value represent the
value of cell âB9â where weâll get the response.
§ The If function starts with âIfâ
(condition) Then (Answer If Condition fulfills) Else (Answer if condition not fulfilled)
and ends with âEnd ifâ.
§ We can read above code as, If âB6â
value is âaâ then response will be âYouâll Preserve Your Friendshipâ otherwise âYou'll
Break the Friendshipâ,
Now letâs check our code.
Weâll type âaâ in Answer cell i.e. âB6â.
Yes, itâs showing the correct response.
Yes, the response is changing.
We can see the code working perfectly.
We can also set the Shortcut Key for
the above macro: -
b) The Macro window will be open and we
can see our macro on left side.
c) Select the same and click on âOptionsâ
tab given on the right side.
d) Here we can set shortcut key and
description of the Macro.
Now, weâll take another example, where weâll cover multiple conditions and If.
Suppose we
have list of students with their respective marks: -
Sr. No.
|
Name of Student
|
Marks
|
1
|
John
|
84
|
2
|
Sachin
|
37
|
3
|
Anna
|
-32
|
4
|
Sania
|
92
|
5
|
Amir
|
38
|
6
|
Monica
|
90
|
And we need to
find out their Grades as per the following Grading Criteria: -
Marks
|
Result
|
0-39
|
D Grade
|
40-60
|
C Grade
|
61-79
|
B Grade
|
80-100
|
A Grade
|
1) Open VBA Editor and insert new
Module as we did in above example.
2) Write or copy the following code: -
Sub MULTIPLE_IF()
If ActiveCell.Previous.Value >= 0 And
ActiveCell.Previous.Value < 40 Then
ActiveCell.Value = "Fail"
ElseIf ActiveCell.Previous.Value >= 40 And
ActiveCell.Previous.Value <= 60 Then
ActiveCell.Value = "C Grade"
ElseIf
ActiveCell.Previous.Value >= 61 And ActiveCell.Previous.Value < 80 Then
ActiveCell.Value = "B Grade"
ElseIf ActiveCell.Previous.Value >= 80 And
ActiveCell.Previous.Value <= 100 Then
ActiveCell.Value = "A Grade"
Else
ActiveCell.Value = "Invalid"
End If
End Sub
Learning Points from above Codes: -
§ In above code as you can see it starts
with Sub and ends with End Sub
§ ActiveCell.value represent the value of active or
selected cell i.e. Grade Columns Cells.
§ ActiveCell.Previous.value represent the value of cell before
the active cell.
Eg.: for âD5â previous cell will be âC5â.
§ We have used âAndâ function
for applying two conditions under single IF.
§ We have used ElseIF for using
multiple options
§ We can read above code as, If the
marks are between 0 â 39 then give result as âFailâ, if it is between 40 â 60,
then as âC Gradeâ, if it is between 61 - 79, then âB Gradeâ, if it is between
81 â 100 then Result as âA Gradeâ and if any other input is there then result
as âINVALIDâ.
Now, letâs try out our code, before that we will set a shortcut key for our Macro by following the previously mentioned steps.
Letâs now try our code.
1) Select âD5â i.e. Grade column for
John.
The marks are above 80 and it has
shown result as âA Gradeâ, itâs working.
Letâs try for Sachin.
Letâs try for Anna where marks are
in Negative
Yes, this code is also working
perfectly.
So, friends I hope we have learned
about the IF AND ELSE conditions today.
You can download the worksheet for your reference.
Weâll come again with another
exciting Macro Function until then,
Namasteđđ
You can watch the video of the above function at:-
No comments:
Post a Comment