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!
How about you make it merely dependent on the first letter of the answer like so:
Option Explicit
Option Compare Text
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
Select Case LCase(Left(Trim(strSeat), 1))
Case "b"
curTicketPrice = 75
Case "p"
curTicketPrice = 30
Case "l"
curTicketPrice = 21
Case "o"
curTicketPrice = 0
Case Else
MsgBox "The location you entered cannot be recognised." & Chr(10) & "Assuming 'Other' as location...."
curTicketPrice = 0
End Select
'pricing results based on seat selection
MsgBox ("The ticket price for a seat in the " & strSeat & " location is: " & Format(curTicketPrice, "$0.00"))
End Sub
As you can see, the user merely needs to get the first letter of the answer right and does not even need to care about upper or lower cases.