
PROVIDING ACCURATE, RELIABLE AND STRAIGHT TO THE POINT KNOWLEDGE ABOUT DIFFERENT IT TOOLS EXPLAINED WITH PRACTICAL EXAMPLES
CONTENTS
GO TO AND SELECT CASE IN VBA
INTRODUCTION-GOTO
We discussed the IF THEN ELSE construct which helps us to take a decision. Similarly there are few more methods to control the execution as we want.
For example , select case , in which we can deal with the coding on the basis of case to case as per the input.
And goto statement which lets us take the control anywhere in the program at once.
Let us check the GOTO Statement first.
Goto statement changes the flow of the program as we want. We just need to give a LABEL to a particular part of the program. This can be called anytime or can be put in the conditions.
GOTO statement can’t take us out of the procedure. Both label and goto should be in the same procedure.
SYNTAX OF GOTO STATEMENT
SYNTAX is a particular way of writing the programs.
Here is the syntax of GOTO STATEMENT
GOTO LABEL
LABEL:
EXAMPLE -GOTO STATEMENT
CODE
‘GYANKOSH.NET Check if its weekend today or not
‘SIMPLE IF
Sub IfExample2()
Dim income As Double
income = InputBox(“Enter your income”)
Dim interest As Integer
If income <= 100000 Then
GoTo NOTELIGIBLE
ElseIf income > 100000 And income <= 1000000 Then
interest = 5
ElseIf income > 1000000 And income <= 2000000 Then
interest = 10
ElseIf income > 2000000 Then
interest = 15
End If
MsgBox (“You are elgible for the loan and the applicable interest will be ” & interest & “%”)
Exit Sub
NOTELIGIBLE:
MsgBox (“You are not elgible for loan”)
End Sub
EXPLANATION
RUNNING THE CODE AND OUTPUT
SELECT CASE IN VBA
INTRODUCTION
Select Case in VBA is again a standard kind of switch case found in different programming languages
“SELECT CASE FACILITATES US TO DEFINE DIFFERENT CASES AND DIRECTLY SWITCH TO THEM FOR FURTHER PROCESSING.”
All the different cases are defined with different kind of processing. The case is selected as per the condition.
The expression is evaluated and the case is referred for processing.
Let us check out the syntax of SELECT CASE in VBA.
SYNTAX OF SELECT CASE
Select Case EXPRESSION_TO_BE_TESTED
[ Case expressionlist-n [ statements-n ]] ‘ CASES ONE BY ONE AND STATEMENTS
[ Case Else [ elsestatements ]] CASE FOR WHICH NO CRITERIA IS GIVEN
End Select
EXPLANATION-SYNTAX
EXAMPLE -SELECT CASE STATEMENT
CODE
‘GYANKOSH.NET SELECT CASE
‘CHECKING OUT THE LOAN ELIGIBILITY AND SHOWING THE APPLICABLE INTEREST RATE
Sub SelectCaseExample()
Dim income As Variant
income = InputBox(“Enter your income”) ‘input for the income
Dim interest As Integer
Select Case income ‘ select case
Case “”
Exit Sub
Case Is <= 100000 ‘ different cases
GoTo NOTELIGIBLE
Case 100000 To 1000000
interest = 5
Case 1000000 To 2000000
interest = 10
Case Else
interest = 15
End Select
MsgBox (“You are elgible for the loan and the applicable interest will be ” & interest & “%”)
Exit Sub
NOTELIGIBLE:
MsgBox (“You are not elgible for loan”)
End Sub
EXPLANATION
RUNNING THE CODE AND OUTPUT
OTHER WAYS TO REACH THIS ARTICLE
WHAT IS EXCEL, HOW EXCEL WORKS, WRITE FORMULA IN EXCEL, EXAMPLE OF USING FORMULA IN EXCEL, ANIMATED EXAMLPE OF USING FORMULA IN EXCEL
YOU MAY LIKE
- HOW TO TROUBLESHOOT A FORMULA IN EXCEL?
- USE OF FONTS, FONT SIZE, INDENTATION, TEXT TO THE RIGHT, TEXT TO THE LEFT, TEXT IN THE MIDDLE ETC.
- how to insert table in excel?
- FOR MORE ARTICLES, CLICK EXCEL OR TYPE IN SEARCH BELOW.