{"id":12136,"date":"2012-04-24T07:16:08","date_gmt":"2012-04-24T07:16:08","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=1605"},"modified":"2022-08-12T11:15:27","modified_gmt":"2022-08-12T11:15:27","slug":"vba-programming-if-then-statement","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2012\/04\/vba-programming-if-then-statement\/","title":{"rendered":"VBA Programming : If Then Statement"},"content":{"rendered":"
This Article is going to teach you about Conditional Programming. By Conditional Programming, I mean, the execution of certain statements based on certain conditions. Sometimes in VBA programming, we want to execute certain statements only when a specific condition is met. If Then Statement<\/strong> helps you achieving this.<\/p>\n \nNow we will discuss about all aspects of If Then<\/strong> Statements. We can use If Then statements in many ways.<\/p>\n If Then Statement – VBA<\/p><\/div> If <\/strong>Condition<\/i> Then<\/strong> <\/p>\n <\/p>\n 1. On Clicking on the Command Button, You will be asked to provide the Score in Input Box. \nFrom the above example, It is clear that, if the condition is True then the message is Displayed. If it is False then nothing is happening. If else Statement – VBA<\/p><\/div>\n As the above image illustrates, a set of condition will be checked and If that is True, then a Set of statements will be executed and if Condition is False then a different set of Statements will be executed.<\/p>\n If <\/strong>Condition<\/i> Then<\/strong> 1. On Clicking on the Command Button, You will be asked to provide the Score in Input Box. You can use multiple If Else statements within If else statements. This is called Nested If else<\/strong> or Multiple If else Conditions<\/strong> Refer the below example:<\/p>\n \nIn the above example you can see, in the first If Part, there is another If else statement is executed. It means when the outer If condition is satisfied then Control will get inside and check the Condition of another If Else statement and based on that it will execute the statements.\n<\/p>\n In Nested If else statements, Control goes to Inner If Else, only when outer If Else is satisfied, otherwise it will ignore the inner If else, even though the condition is satisfied.<\/p>\n \nIt is not a good Idea to use Nesting of If Else Conditions for more than 3-4 times. It becomes very difficult to understand the code. Also it takes long time to execute all the conditions. To overcome this problem, you should use Select Case Statement<\/strong>.<\/p>\nIf Then Statement <\/h1>\n
\n
\nAs the above image illustrates, a set of conditions will be checked and If that is True, then a Set of statements will be executed otherwise it will not be executed.<\/p>\nSyntax:<\/h3>\n
\n
\nStatement 1….
\nStatement 2….
\n….
\n….<\/i> End If<\/strong><\/p>\nExample:<\/h3>\n
\r\nPrivate Sub CommandButton1_Click()\r\n\tDim Score As Integer\r\n\tScore = InputBox(\"Enter your Score\", \"Enter Score\")\r\n\tIf Score >= 45 Then\r\n\t\tMsgBox (\"You are Passed\")\r\n\tEnd If\r\nEnd Sub\r\n<\/code><\/pre>\n
Explanation of the Above Code:<\/h4>\n
\n2. Score will be Compared if it is Greater than or Equal to 45 (>=45)<\/i>
\n3. If the above condition is true( if you have entered some greater number than 45) then a Message will be displayed as You are passed<\/strong>
\n4. If you have entered less than 45, then the condition will fail and in that case No message will be displayed.<\/p>\n
\nSometimes, we are in a situation, where we want to execute certain Statements, if the condition is True and a set of Different Statements, when it is False. To achieve this, we use If Else <\/strong> Statement.\n<\/p>\nIf else Statement: <\/h1>\n
Syntax:<\/h2>\n
\n
\nStatement 1….
\nStatement 2….
\n….
\n….<\/i>Else<\/strong>
\nStatement 1….
\nStatement 2….
\n….
\n….<\/i>
\n End If<\/strong><\/p>\nExample:<\/h2>\n
\r\nPrivate Sub CommandButton1_Click()\r\n\tDim Score As Integer\r\n\tScore = InputBox(\"Enter your Score\", \"Enter Score\")\r\n\tIf Score >= 45 Then\r\n\t\tMsgBox (\"You are Passed\")\r\n\tElse\r\n\t\tMsgBox (\"You are Failed\")\r\n\tEnd If\r\nEnd Sub\r\n<\/code><\/pre>\n
Explanation of the Above Code:<\/h4>\n
\n2. Score will be Compared if it is Greater than or Equal to 45 (>=45)<\/i>
\n3. If the above condition is true( if you have entered some greater number than 45) then a Message will be displayed as You are passed<\/strong>
\n4. If you have entered less than 45, then the condition will fail and in that case the message which is there in Else<\/strong> part will be displayed. You are Failed<\/strong><\/p>\nNested If Else Condition (Multiple If else Statement)<\/h1>\n
\r\nPrivate Sub CommandButton1_Click()\r\n\tDim Score As Integer\r\n\tScore = InputBox(\"Enter your Score\", \"Enter Score\")\r\n\tIf Score >= 45 Then\r\n\t\tIf Score >= 60 Then\r\n\t\t\tMsgBox (\"You are Passed in First Division\")\r\n\t\tElse\r\n\t\t\tMsgBox (\"You are Passed\")\r\n\t\tEnd If\r\n\tElse\r\n\t\tMsgBox (\"You are Failed\")\r\n\tEnd If\r\nEnd Sub\r\n\r\n<\/code><\/pre>\n
Note:<\/h3>\n