Updated June 12, 2023
PART- 9 – VBA Programming – IF then Statement
Transcript For Video – VBA Programming – IF then Statement
Video on VBA Programming – IF then Statement – Hello friends welcome back now that we have looked that at least some basic fundamental elements of VBA and Macros and also understood have the debug and the watch window and immediate window actually looks like lets actually create basic Macros try to see if we are able to kind of achieve the two objectives number one is weather we are able to create the Macros correctly and number two is that if there are certain condition how can we right some kind of codes so that you know we can incorporate those conditions the conditions essentially mean that let say if ball is equal a red color then prompt the certain then do certain then do certain action or may be if pen is black then do something so in a these are certain conditions so that’s what this conditions are all about and how do we in corporate that in a macros so probably what will do is it will take case which is listen to here what we will do is in a cell A1 which they define they cell (1,1) this is basically cell number A1 we will right are name ok and the cell number A2 which is basically cells (2,1). Value I am sure you are able to follow this I will right they age of the person what needs to be done now what need to be done the output should be something like this it should prompt the name and tell us whether the person is a minor or let say Major or a Senior citizen now I will give a some conditions or how the bifurcation would be a like lets define a minor as someone whose age is less than 18 ok a major would be someone who has age of greater than 18 just we greater than 18 and less than let say age here and the senior citizen would be the age which is greater than 60 there should be a massage prompt and tells us the act high whatever the name is you are a Major a Minor senior citizen so you know based on these this conditions so what is the objective in this two cells here A1 and A2 we will put a cross the names and the age and the Macros should automatically prompt us with message that high David you are a minor high David you are a senior or senior citizen on something a that’s all so the this whole objective but before doing that lets kind of you known run through a virtual code and the see if that make sense a for implementing these conditions right Minor major and senior citizen we will be using if function so obviously we need to test whether the age is greater than 18 or less than 18 or you know greater than 60 so if is basically use to check certain conditions so if as we do this same thing in excel the same aspect of conditions actually apply here as well so what mean what to do here if me need just check if age is less than 18 then prompt the message as high name you are a minor else if age is less than 60 so what will happen in this case if the age less than 18 then this message prompted if age is not less than 18 it will the move further and it will evaluate this if functions so here then message should be Hi name you are a major else again if this condition is also not true then a probably there is no requirement because if here if this two or not a true definitely the person would line the third zone so the message should be something like this hi name you are a senior citizen so these the whole objective of our exercise and the virtual algorithm would be something like this ok so let’s try and implement this in our case and let’s see it works out well for us so we have open a new worksheet called book one and I will go to the developers tab click here and usual basic and obviously we go into the VB editor, now I am going to click here on sheet one ok so going to define a new functions so new functions start with sub and the name would be something like this category so I just want to understand which category the person lies into to maybe you know Age category so moment I click enter you know automatically this brackets and end sub actually comes into the picture sheet don’t have to worry about things this VBA is kind of very intuitive and very intelligent to fill up the remaining things lets the start by implementing what me wanted to do here so we want to first info most look at the name and the age obviously name is a strength and age could be and integer or be decimal so for the time being lets assume that name is string and age would be integer age so I define dim age as integer you remember we had just done this in a earlier case I will explain you the different kind of wearable other video all together but for the time being just insure that you are kind of understanding the whole concept here I am defining name as string variable because it is string ok now this is what I do I just shorten this length so that I can see this else and the excel as well now what happen is I want to have the cell 1,1 as name and cell 2 1 as age so I will define this name is equal to cells 1,1. Value and age as cells 2, 1. Value Basically, what we are saying is here on the left-hand side, will right the name, and in this column in this cell, will right the age, so this is name, this is age, so that’s what has been defined, and this wearable will gate assign to the respective cells.