VBA example - Excel 2007: VBA Select Case


VBA Select Case

In this example, We use VBA Select Case statement to display a grade of a total score. The following conditions are applied to the total score and its grade:

   -If total score>=90 and <=100, then the grade is A.
   -If total score>=80 and <=89, then the grade is B.
   -If total score>=70 and <=79, then the grade is C.
   -If total score>=60 and <=69, then the grade is D.
   -If total score>=50 and <=59, the the grade is E.
   -If total score<50, then the grade if F.

  


   To run this example VBA code, you will need a Form, one TextBox, and two Labels.

VBA for Excel: VBA code example of using Select Case statment show the grade of a total score.


VBA code to calculate the grade of input total score:

Private Sub TextBox1_Change()    
 
Dim sc As Integer
   
 
If TextBox1.Text <> "" Then
       
   
sc = CInt(TextBox1.Text)
   
 
End If
    
 
If sc >= 0 And sc <= 100 Then
   
   
Select Case sc
       
   
Case 90 To 100: Label2.Caption = "A"
       
   
Case 80 To 89: Label2.Caption = "B"
       
   
Case 70 To 79: Label2.Caption = "C"
       
   
Case 60 To 69: Label2.Caption = "D"
       
   
Case 50 To 59: Label2.Caption = "E"
   
   
Case Else: Label2.Caption = "F"
           
   
End Select
    Else: Label2.Caption = "Invalid!"    
  
End If
   
End Sub



Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.