Vba if Condition Not True Wait and Try Again Vba if Condition False Wait and Try Again
Return to VBA Code Examples
VBA Multiple (Nested) If Statements
This tutorial volition show yous how to use nested If statements in VBA
If statements permit you lot to exam for a single condition in VBA to run across if the condition is Truthful or False, and depending on the respond, the code volition move in the direction of the true statement or the false statement.
A Single IF statement
Sub TestIf Dim x as Integer x = x If 10 = 10 so 'if ten is 10, the condition is true MsgBox ten is ten " Else 'if x is not 10, the condition is imitation Msgbox " ten is not 10" End If End Sub |
Nested IFs explained
A Nested If allows you lot to put multiple weather condition Within each of the True and/or Fake statements of the original If.
i 2 3 four v half-dozen seven 8 9 10 11 12 thirteen fourteen xv 16 17 18 19 20 21 22 23 24 25 26 | Sub TestNestedIf ( ) Dim x every bit Integer Dim y every bit Integer Dim z every bit Integer ten = 10 y = nine z = 8 If x = 10 then 'if x is 10, the condition is truthful so test for y If y = 8 then MsgBox "y is nine" Else 'if y is not 10, the condition is false Msgbox "y is not ix" Cease If Else 'if x is not 10 then the condition is false, then lets' exam for z If z = 8then MsgBox "z is 8" Else 'if z is not 8, the status is false Msgbox "z is not x" End If 'some other Terminate If is needed to close the original if End If End Sub |
Indenting your code when yous write it always good exercise as it makes the code piece of cake to read and follow when you accept to come back to it at some stage, or when another programmer has to read it.
We could besides create a user designed function (UDF) and telephone call the values of some cells from Excel into the office using parameters.
1 two 3 4 5 6 7 eight 9 10 11 12 thirteen fourteen 15 16 17 18 nineteen xx | Function GetIf ( x equally Integer , y equally Integer , z equally Integer ) every bit String If ten = 10 then 'if x is 10, the status is true and so examination for y If y = 8 and so GetIf= "y is ix" Else 'if y is not 10, the condition is false GetIf="y is not 9" End If Else 'if x is not 10 then the condition is false, and so lets' test for z If z = 8 then GetIf="z is 8" Else 'if z is not 8, the condition is false GetIf="z is not 10" Terminate If 'another End If is needed to close the original if Stop If End Function |
Nested If Practical Example
Consider the following function:
1 2 3 4 5 6 7 viii 9 10 11 12 13 fourteen fifteen 16 17 18 19 twenty 21 22 | Function GetDiscount ( dblPrice As Double ) As Double If dblPrice >= 1000 Then 'if the price is greater than 1000, assign a disbelieve If dblPrice >= 2000 And then 'if greater than 2000, give 10% disbelieve GetDiscount = dblPrice * 0.1 Else 'otherwise give 5% discount GetDiscount = dblPrice * 0.05 Terminate If 'if the price is non greater than 1000 Else 'if greater than 500, requite 2.5% discount If dblPrice >= 500 Then GetDiscount = dblPrice * 0.025 Else 'otherwise no discount GetDiscount = 0 Stop If 'another Stop If is needed to close the original if End If Cease Function |
Using this function in an Excel sail, we can test to run into the total price for an order, and apply different discounts depending on that full.
Using ElseIf
ElseIf enables u.s.a. to simplify your lawmaking as it only moved down to the 2d if statement if the first i returns a false.
Part GetDiscount ( dblPrice As Double ) As Double 'employ else if to cutting downward on writing code If dblPrice >= 2000 So GetDiscount = dblPrice * 0.1 ElseIf dblPrice >= 1000 Then GetDiscount = dblPrice * 0.075 ElseIf dblPrice >= 500 And then GetDiscount = dblPrice * 0.05 ElseIf dblPrice >= 200 Then GetDiscount = dblPrice * 0.025 ElseIf dblPrice >= 100 Then GetDiscount = dblPrice * 0.01 Else GetDiscount = 0 End If End Function |
Using a Example Argument
We tin can besides use a Case Argument to reach the same outcome.
1 2 3 iv 5 6 seven 8 nine 10 11 12 13 14 15 16 17 | Function GetDiscount ( dblPrice As Double ) As Double Select Case dblPrice 'this instance statement has six different discount levels Case Is >= 2000 GetDiscount = dblPrice * 0.1 Example Is >= k GetDiscount = dblPrice * 0.075 Case Is >= 500 GetDiscount = dblPrice * 0.05 Case Is >= 200 GetDiscount = dblPrice * 0.025 Example Is >= 100 GetDiscount = dblPrice * 0.01 Case Else GetDiscount = 0 Cease Select End Function |
VBA Coding Made Piece of cake
Terminate searching for VBA code online. Learn more than well-nigh AutoMacro - A VBA Code Builder that allows beginners to lawmaking procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Larn More!
Source: https://www.automateexcel.com/vba/multiple-nested-if-statements/
0 Response to "Vba if Condition Not True Wait and Try Again Vba if Condition False Wait and Try Again"
Post a Comment