CAPITULO III MARCO NORMATIVO: INTERÉS LEGÍTIMO Y PROTECCIÓN AL MEDIO
III.4 Ley General de Equilibrio Ecológico y Protección al Ambiente
Excel VBA Conditional Operators
In
In the previous lessonsthe previous lessonson If Statements, you've already used one conditional operator - theon If Statements, you've already used one conditional operator - the equal sign. But there are others. Here's a list:
equal sign. But there are others. Here's a list:
By using the various Operators, you can set up more sophisticated conditions for your If By using the various Operators, you can set up more sophisticated conditions for your If Statements.
Statements.
Add another Sub to your code from the
Add another Sub to your code from the previous lesson previous lessonss.. Call it Call it If_Test_3If_Test_3. As the code for. As the code for your new Sub, add the following:
your new Sub, add the following:
Dim MyNummber As Integer Dim MyNummber As Integer MyNumber = 10
MyNumber = 10
If MyNumber < 20 Then If MyNumber < 20 Then
MsgBox MyNumber & " is L
MsgBox MyNumber & " is Less than 20"ess than 20" End If
End If
Your coding window should look like this: Your coding window should look like this:
Again, we've set up an Integer variable called
Again, we've set up an Integer variable calledMyNumberMyNumber and stored a value of 10 in it. and stored a value of 10 in it. Look at the first line of the If Statement, though:
Look at the first line of the If Statement, though:
If MyNumber < 20 Then If MyNumber < 20 Then
If you consult the table above, you'll see the
If you consult the table above, you'll see the<< symbol means symbol meansLess ThanLess Than. So the condition to. So the condition to test is, "If MyNumber is less than 20". If
test is, "If MyNumber is less than 20". IfMyNumberMyNumber is indeed less than 20 then the is indeed less than 20 then the condition evaluates to TRUE. In which case, the code between
condition evaluates to TRUE. In which case, the code betweenIf If and and End If End If gets executed. If gets executed. If it's FALSE then VBA will skip to any lines after
it's FALSE then VBA will skip to any lines after End If End If ..
One other thing to note about the code is the message box line: One other thing to note about the code is the message box line:
MsgBox MyNumber & " is Less than 20" MsgBox MyNumber & " is Less than 20"
After MsgBox we have the variable
After MsgBox we have the variableMyNumberMyNumber then a space then an ampersand ( then a space then an ampersand (&&)) symbol. The
symbol. The&&symbol is used to concatenate (join together) things. After thesymbol is used to concatenate (join together) things. After the&& symbol we symbol we then have another space followed by some direct text in double quotes. So whatever is in the then have another space followed by some direct text in double quotes. So whatever is in the variable
variableMyNumberMyNumber will get joined with the direct text in double quotes. will get joined with the direct text in double quotes.
Run your code and you should see the message box display the following: Run your code and you should see the message box display the following:
Return to your coding window and change
Return to your coding window and changeMyNumber = 10MyNumber = 10 on the second lineon the second line to
toMyNumber = 20MyNumber = 20..
Run your code again and you'll find that nothing happens. Run your code again and you'll find that nothing happens.
The reason nothing happens is that 20 is not less than 20, so the If Statement is FALSE. We The reason nothing happens is that 20 is not less than 20, so the If Statement is FALSE. We haven't got
Now change the
Now change the << symbol to symbol to <=<=. The. The <=<= symbols together mean "Less than or equal to". symbols together mean "Less than or equal to". Change you message to this:
Change you message to this:
MsgBox MyNumber & " is Less than or equal to 20" MsgBox MyNumber & " is Less than or equal to 20"
Your code should now be the same as ours below: Your code should now be the same as ours below:
Dim MyNummber As Integer Dim MyNummber As Integer MyNumber = 20
MyNumber = 20
If MyNumber <= 20 Then If MyNumber <= 20 Then
MsgBox MyNumber & " is Less than or equal to 20" MsgBox MyNumber & " is Less than or equal to 20" End If
End If
When you run your code, here's what the message box will look like When you run your code, here's what the message box will look like
Click OK and return to your code. Change the
Click OK and return to your code. Change the<=<= symbol tosymbol to>=>=. The. The >=>= symbols together symbols together mean "Greater than or equal to".
mean "Greater than or equal to".
Change your message text to this: Change your message text to this:
MsgBox MyNumber & " is Greater than or equal to 20" MsgBox MyNumber & " is Greater than or equal to 20"
When you run your code, you'll see the message box appear again: When you run your code, you'll see the message box appear again:
To test out the
To test out theGreater ThanGreater Than sign by itself delete the = sign next to the > symbol. Change sign by itself delete the = sign next to the > symbol. Change MyNumber to 25. Amend the text in your message. Your code will then look like this: MyNumber to 25. Amend the text in your message. Your code will then look like this:
Dim MyNummber As Integer Dim MyNummber As Integer
MyNumber = 25
If MyNumber > 20 Then
MsgBox MyNumber & " is Greater than 20" End If
Run your code again to see the new message:
You can have ElseIf and Else parts with your Conditional Logic. Take the following as an example:
Dim MyNummber As Integer MyNumber = 19
If MyNumber = 20 Then
MsgBox MyNumber & " is 20" ElseIf MyNum > 20
MsgBox MyNumber & " is Greater Than 20" Else
MsgBox MyNumber & " is below 20" End If
In the code above, we have anIf and ElseIf andElse. The If Statement tests for a value of exactly 20. TheElseIf tests if the variableMyNumber is Greater Than 20. The Else part catches any other value. Try it out and see how it works. Change the value of the
variable MyNumber and run your code again.