VBA PLAYLIST: • Learn Excel VBA (Visual Basic for Applicat...
This eighth clip in this VBA video series explains how to create a conditional statement in Visual Basic for Applications (VBA) for Microsoft Excel.
hello guys my name is car and actually in this video we'll be discussing if condition in VD other than if condition I will also discuss some important functions in VBA so but mainly this beauty will be about if condition so for this video I'm using this hypothetical data that I have just created for you guys so this data is very small we have so suppose we collected the data of nine students we have this iDVD able then we have the age of those students and then we have a variable which is the score then they go to score in any test so the first thing that I want to create is the variable which will be saying that is this this in the D column I want to see that those words are adult or not so if the student is above 18 years it will be considered a student and the student will not be above 80 will not be considered as adult so you want to create that categorical variable here in quality so first I will do that in order to do that I am already in Developer tab I will go to redo this thing so I'm working on the book 1 so I will go to this icon down arrow and will start mine what you want so you can change the name of the module by clicking here but I want to name it more in 1 so I will not change so I will start creating my macro now so I am start as usual it's up then I will give give this macro name so I will be calling it if in condition so if underscore condition so everything now it will appear in this within these two lines within sub and this tensor so as I said that I will be making an if condition in this video so first what I will do I have also want to at this cell so I will be saying so I go edit in D column so I will be using the function cells Row 1 coma called Sara : d so inverted commas D then bracket closed and then I will be calling that cell he can't so is cap in each category so if I execute this macro at this stage you can see that now I have two here now I've written down here that this period we call as this column will what is true this column will be called as age category and after that what I need to do if condition but before if condition I need to run a move one of the reason of constructing a run before implementing the if condition is because if you go back to my excel sheet so actually I have to go to each cell to see that the guy is above 18 or not so then first I'm go to this room this cell then I will go to here to see who that above I think that the student is about 18 or not then I will print the value here then I will come here and check it again that the student is above 18 or not if it's about dating I will be saying is too dull so then I have one by one I have to go to each row and keep soon updating these cells accordingly so that's why I have to construct a move so I will go back and then I will make my move for then I will be using a local name this high I will be starting from two because the first row is actually just the column name or you can call them variable names so I am saying I is equal to two to the last row is the 10 and then I getting and then it's a good habit to whenever you start a loop also and the loop by writing down next I and you can write down all your commands within this loop no so now I will write down the if condition so I have the same in cells
and then a my row index will be I because I the I have the row index which keeps on changing according to the seal and then I'd missing comma and then the I will be stating this column the column LBD because my if condition is relying on this column so I'll be saying be and then I will sing a if this cell value is greater than equal to 80 so greater than equal to 80 and then I will be saying so then and then you have to press ENTER and after that if I'm saying that if this cell value which is in the quality is 18 then go to D cell D D 2 so in which will be sense I again my row index will keep on changing because first I have to go here then here then here that's why I have to say I coma my column indexes this time is D and then 3 equal to I will be same pattern and then you write if condition you have to tell VD the trendy at what stage you if condition is ending so I have to press ENTER and then I will write down and ever enter so now our if condition is starting from there so we are saying if this this the cells in the B are greater than equal to 18 then go to column D and print adult so now I've execute this code let's see what happens so as you can see that when the value here is a 18 or above 18 we have written down adult in the column D but when the value is not true above 18 or equal to 18 there is nothing printed here so in order that smooth who want that if the condition is satisfied then print a dirt as print something else