Monday, June 30, 2014

How to use the CASE and the INPUTBOX in Excel VBA ?

Below is the example of the input box and the Case, used in excel VBA to check, the letter belongs to which grade description.

Copy paste the below code to any of the Excel VBA module and run it. you will get the pop-up window to enter the input letter. Enter the letter and click OK.


You will get the message box with the letter is for description value.


Sub testcase()


Dim grade As String

grade = "A"
grade = InputBox("Enter the Grade from A to D")
Select Case grade

Case "A"

MsgBox grade & " is for High Distinction"

Case "B"

MsgBox grade & " is for Credit"

Case "C"

MsgBox grade & " is for Pass"

Case Else

MsgBox grade & " is for Fail"

End Select

End Sub

Using the code examples in the above macro, this can be modified to do different tasks at different situations.

e.g., if you want to write the grade against the marks in excel, we can modify the above code adding the loop code and mapping the grades to marks.

Done...

No comments:

Post a Comment