Exercise: Teleport price Please use VBA for Microsoft Excel, thank you! The carg
ID: 3852839 • Letter: E
Question
Exercise: Teleport price
Please use VBA for Microsoft Excel, thank you!
The cargo teleporter from Earth to Mars operates between 3 am and 11 am every day. Write a program that computes the price of sending cargo at a particular time. The price per kilo changes every hour. Teleports from Earth to Mars have different prices than teleports from Mars to Earth.
Here’s a sample:
The user enters the time of the transport, kilos, and whether the destination is Earth or Mars. The program computes the price. The PPK (price per kilo) table is shown.
You can assume that users don’t make any typing mistakes.
Hint: Time is entered as hour, then a space, then minutes. Use the InStr function to find the space. The characters to the left of that give you the hour.
Here is my current code.that I need help figuring out.
Option Explicit
Private Sub cmdCompute_Click()
'Vars
Dim Kilos As Integer
Dim tm1 As Integer
Dim Price As Integer
Dim Time As String
Dim Destination As String
'Get Data
Time = Cells(3, 2)
Kilos = Cells(7, 2)
Destination = Cells(9, 2)
Price = Left([Time], (InStr(1, [Time], " ")) - 1)
'MsgBox (tm1)
Select Case Val(tm1)
Case 3
'Compute
If Destination = "M" Then
Price = Kilos * Cells(17, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(17, 4)
End If
End If
Case 4
If Destination = "M" Then
Price = Kilos * Cells(18, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(18, 4)
End If
End If
Case 5
If Destination = "M" Then
Price = Kilos * Cells(19, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(19, 4)
End If
End If
Case 6
If Destination = "M" Then
Price = Kilos * Cells(20, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(20, 4)
End If
End If
Case 7
If Destination = "M" Then
Price = Kilos * Cells(21, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(21, 4)
End If
End If
Case 8
If Destination = "M" Then
Price = Kilos * Cells(22, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(22, 4)
End If
End If
Case 9
If Destination = "M" Then
Price = Kilos * Cells(23, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(23, 4)
End If
End If
Case 10
If Destination = "M" Then
Price = Kilos * Cells(24, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(24, 4)
End If
End If
Case 11
If Destination = "M" Then
Price = Kilos * Cells(24, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(24, 4)
End If
End If
End Select
Cells(14, 1).Select
Cells(14, 1) = Price
End Sub
Explanation / Answer
Option Explicit
Private Sub cmdCompute_Click()
'Vars
Dim Kilos As Integer
Dim tm1 As Integer
Dim Price As Integer
Dim Time As String
Dim Destination As String
'Get Data
Time = Cells(3, 2)
Kilos = Cells(7, 2)
Destination = Cells(9, 2)
Price = Left([Time], (InStr(1, [Time], " ")) - 1)
'MsgBox (tm1)
Select Case Val(tm1)
Case 3
'Compute
If Destination = "M" Then
Price = Kilos * Cells(17, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(17, 4)
End If
End If
Case 4
If Destination = "M" Then
Price = Kilos * Cells(18, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(18, 4)
End If
End If
Case 5
If Destination = "M" Then
Price = Kilos * Cells(19, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(19, 4)
End If
End If
Case 6
If Destination = "M" Then
Price = Kilos * Cells(20, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(20, 4)
End If
End If
Case 7
If Destination = "M" Then
Price = Kilos * Cells(21, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(21, 4)
End If
End If
Case 8
If Destination = "M" Then
Price = Kilos * Cells(22, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(22, 4)
End If
End If
Case 9
If Destination = "M" Then
Price = Kilos * Cells(23, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(23, 4)
End If
End If
Case 10
If Destination = "M" Then
Price = Kilos * Cells(24, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(24, 4)
End If
End If
Case 11
If Destination = "M" Then
Price = Kilos * Cells(24, 3)
Else
If Destination = "E" Then
Price = Kilos * Cells(24, 4)
End If
End If
End Select
Cells(14, 1).Select
Cells(14, 1) = Price
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.