• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Using Select Case

The If statement can get quite cumbersome to read and understand if there are many ElseIf clauses. To simplify your code while maintaining the same basic structure, you can use the Select Case statement. Here’s the previous example rewritten as a Select Case:

Function GetDayName(dtmDayToCheck) As String
  ' Returns the day of the specified date
  Select Case DatePart("w", dtmDayToCheck)
    Case 1
      GetDayName = "Sunday"
    Case 2
      GetDayName = "Monday"
    Case 3
      GetDayName = "Tuesday"
    Case 4
      GetDayName = "Wednesday"
    Case 5
      GetDayName = "Thursday"
    Case 6
      GetDayName = "Friday"
    Case Else
      GetDayName = "Saturday"
  End Select
End Function


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint