为什么我的这个VBA酒店收费宏分配的MsgBox输出错误了?

时间:2017-04-04 17:03:24

标签: excel vba excel-vba if-statement global-variables

基本上出于某种原因,当我遇到这个问题时,它不会显示房间类型(应该是Studio,Moderate,Luxe,Deluxe或Suite),并且它不会显示我的每日费率或总数率。我已经在这个任务中待了好几个小时,我无法弄清楚这似乎是什么问题。我认为这可能与我的ElseIf声明或全局常量有关,但我不确定我做错了什么。

2 个答案:

答案 0 :(得分:2)

您正尝试使用And运算符为变量设置值。这不正确,因为ng-repeat-start运算符是ng-repeat-end

  

对两个布尔表达式执行逻辑连接,或者a   两个数字表达式的按位连接。

<强>即:

当你写:

And

此行logical operator的输出将为If RmType = 1 Then RmCost = Studio And RmName = "Studio" RmCost = Studio And RmName = "Studio"

您没有为TrueFalse设置任何值!

所以,而不是:

RmCost

您应该使用:

RmName

而不是:

If RmType = 1 Then
        RmCost = Studio And RmName = "Studio"
ElseIf RmType = 2 Then
        RmCost = Moderate And RmName = "Moderate"

使用:

If RmType = 1 Then
    RmCost = Studio
    RmName = "Studio"
ElseIf RmType = 2 Then
    RmCost = Moderate
    RmName = "Moderate"

答案 1 :(得分:0)

错误的关键部分的原因已在@ dot.Py的评论和答案中得到解答(当我输入此代码时)。

我刚刚添加了一些其他更正(缺少变量声明,将Integer替换为Long - 读取HERE,然后添加MSDN),并替换多个If Select Case {/ 1}}。

<强>代码

Option Explicit

Global Const Studio As Long = 75, Moderate As Long = 100, Luxe As Long = 150, Deluxe As Long = 300, Suite As Long = 500

Sub Charge()

Dim Tip As Double
Dim Total As Long
Dim RmName As String
Dim RmCost As Long
Dim RmType As Long
Dim NumDays As Long

RmType = InputBox("What room type would you like ? Enter '1' for Studio, '2' for Moderate, '3' for Luxe, '4' for Deluxe, and '5' for Suite.")

Select Case RmType
    Case 1
        RmCost = Studio
        RmName = "Studio"
    Case 2
        RmCost = Moderate
        RmName = "Moderate"
    Case 3
        RmCost = Luxe
        RmName = "Luxe"
    Case 4
        RmCost = Deluxe
        RmName = "Deluxe"
    Case 5
        RmCost = Suite
        RmName = "Suite"
    Case Is < 1, Is > 5
        MsgBox "Incorrect Data Entry"
        Exit Sub
End Select

NumDays = InputBox("How many days would you like to stay ?")    
Tip = InputBox("Enter in desired gratuity. For x% enter x.")
Tip = Tip / 100    
Total = RmCost * NumDays * Tip

If RmType = 1 And NumDays > 2 Then
    RmType = 2
    MsgBox "Studio type isn't available for more than 2 nights. You're upgraded to a Moderate option." & vbNewLine & "Room Type: " & _
        RmName & ". Daily rate: " & RmCost & ". Number of days: " & NumDays & "." & _
        "Total Charge: " & Total & vbCrLf & vbCrLf & "Note that the charge includes the " & _
        Tip * 100 & "% tip."
Else
    MsgBox "Room Type: " & RmName & ". Daily rate: " & RmCost & ". Number of days: " & _
        NumDays & ". Total Charge: " & Total & vbCrLf & vbCrLf & _
        "Note that the charge includes the " & Tip * 100 & "% tip."
End If

End Sub