Select Case
One can use the Select Case structure instead of multiple if-then statements.
Add the following lines of code and place the command button on the worksheet.
1. Declaration of Variables
Dim marks As Integer, result As String
2. Initialization of Variables
marks = Range("A1").Value
3. Addition of Select Case Structure
Select Case marks
Case Is >= 80
result = "very good"
Case Is >= 70
result = "good"
Case Is >= 60
result = "sufficient"
Case Else
result = "insufficient"
End Select
4. Result
Range("B1").Value = result
Mod Operator
The remainder of a division is the resultant of the mod operator.
Add the following lines of code and place the command button on the worksheet
MsgBox 6 Mod 2
Result:
Prime Number Checker
This is used to find whether the given number is prime or not.
Add the following lines of code and place the command button on the worksheet.
1. Declaration of Variables
Dim divisor As Integer, num As Long, i As Long
2. Initialization of Variables
divisor = 0
num = InputBox("Enter a number")
3. Starting of Next Loop
For i = 1 To num
4. Calculating the number of Divisors
If num Mod i = 0 Then
divisor = divisor + 1
End If
5. Closing the loop
Next i
6. Displaying the MsgBox
If divisor = 2 Then
MsgBox number & " a prime number"
Else
MsgBox number & " not a prime number"
End If
Result:
Logical Operators
The most commonly used logical operators are AND, OR, NOT.
Logical AND Operator
Add the following lines of code and place the command button on the worksheet.
Dim marks1 As Integer, marks2 As Integer, result As String
marks1 = Range("A1").Value
marks2 = Range("B1").Value
If marks1 >= 50 And marks2 > 1 Then
result = "pass"
Else
result = "fail"
End If
Range("C1").Value = result
Result:
Logical OR Operator
Add the following lines of code and place the command button on the worksheet.
Dim marks1 As Integer, marks2 As Integer, result As String
marks1 = Range("A1").Value
marks2 = Range("B1").Value
If marks1 >= 50 Or marks2 > 1 Then
result = "pass"
Else
result = "fail"
End If
Range("C1").Value = result
Result:
Logical NOT Operator
Add the following lines of code and place the command button on the worksheet
Dim marks1 As Integer, marks2 As Integer, result As String
marks1 = Range("A1").Value
marks2 = Range("B1").Value
If marks1 >= 50 And Not marks2 = 1 Then
result = "pass"
Else
result = "fail"
End If
Range("C1").Value = result
Result:
FAQs
1. What is the use of the If-Then statement?
If-Then statement needs the execution of specific lines of code when a particular condition is met.
2. What is the resultant of the Mod Operator?
The remainder of a division is the resultant of the mod operator.
3. Name different logical operators.
The three different logical operators are AND, OR, NOT.
4. What is the use of the Prime Checker?
This is used to find whether the given number is prime or not.
5. Which is the better option for using multiple if-then statements?
Select Case Structure
Key Takeaways
Congratulations on finishing the blog!! After reading this blog, you will grasp the concept of the If-Then statement.
If you are preparing yourself for the top tech companies, don't worry. Coding Ninjas has your back. Visit this link for a well-defined and structured material that will help you provide access to knowledge in every domain.