samedi 27 février 2016

VBA Input Box and If Statement - Catching user misspellings

I'm practicing some VBA code, and I'm trying to write a code that will display the appropriate price in a message box for various types of seat locations that have their assigned price. I also want to make sure I use an If statement for this code.

Seat Location:

Box $75

Pavilion $30

Lawn $21

What I have so far is an input box that asks the user to enter the seat location, and a message box will come up with the assigned price. My problem is figuring out how to display the appropriate price when the user inadvertently misspells the seat location. The code I have right now works if everything is spelled correctly, but how do I make it work even if the user misspelled the seat location ex. Instead of Pavilion they enter Pavillion.

Here is the code I have so far.

    Option Explicit
    Public Sub ConcertPricing()
    'declare variables
    Dim strSeat As String
    Dim curTicketPrice As Currency

    'ask user for desired seat location
    strSeat = InputBox("Enter seat location", "Seat Location")
   'if statement that assigns appropriate pricing according to seat selection
   If strSeat = "Box" Then
    curTicketPrice = 75
    Else
       If strSeat = "Pavilion" Then
       curTicketPrice = 30
       Else
          If strSeat = "Lawn" Then
          curTicketPrice = 21
          Else
             If strSeat = "Other" Then
             curTicketPrice = 0
             End If
          End If
       End If
    End If

    'pricing results based on seat selection
    MsgBox ("The ticket price for a seat in the " & strSeat & " location is:    " & Format(curTicketPrice, "$0.00"))

    End Sub

Thank you!

Aucun commentaire:

Enregistrer un commentaire