【问题标题】:MSGBOX not working properly on order clickMSGBOX 在订单点击时无法正常工作
【发布时间】:2019-03-26 13:39:02
【问题描述】:

我有一个订单按钮,可以计算出产品的价格,然后将订单详细信息呈现给用户。但是,我的 MSG BOX 不工作了。

Sub ButtonOrder_Click()
    Dim TotalOrdered As Integer
    Dim Price As Single
    Dim StrMsg As String
    Const OrderPrice = 2.5
    Const TaxRate = 0.06
    Const TaxRateMultiplier = 1.06

    With ThisWorkbook.Sheets("Form")
        If (Len(.Range("B2")) = 0) Then
            Range("B2") = InputBox("Enter your name:  ")
        ElseIf (Len(.Range("B3")) = 0) Then
            Range("B3") = InputBox("Please enter your email: )
        ElseIf (Len(.Range("B4")) = 0) Then
            Range("B4") = InputBox("Please Enter Chocolate Amount:  )
        ElseIf (Len(.Range("B5")) = 0) Then
            Range("B5") = InputBox("Please Enter Vanilla Amount : )
        ElseIf (Len(.Range("B6")) = 0) Then
            Range("B6") = InputBox("Please Enter Strawberry Amount: )
        Else
            TotalOrdered = Range("B4").Value + Range("B5").Value + Range("B6").Value
            Exit Sub
        End If
    End With

    'goes through checking the order and number and discount amount
    Select Case Price
        Case TotalOrdered >= 6 And TotalOrdered <= 10
            Price = TotalOrdered * OrderPrice * 0.95
        Case TotalOrdered >= 11 And TotalOrdered <= 20
            Price = TotalOrdered * OrderPrice * 0.9
        Case TotalOrdered >= 21
            Price = TotalOrdered * OrderPrice * 0.8
        Case Else 'less than 6
            Price = TotalOrdered * OrderPrice
           End Select

    'I incorporated the unit price with discount so it is more informative for the customer
    StrMsg = ("Unit Price: $" & Price / TotalOrdered _ 'format function from HW 2
    & "Quantity: " & TotalOrdered _
    & "Tax Rate: $" & TaxRate _
    & "Final Total Price: " & Price * TaxRateMultiplier)

End Sub

【问题讨论】:

  • 我看不到任何消息框,但我确实看到了缺少引号的输入框。 ("Please enter your email: ) 应该是 ("Please enter your email: ") 等。您还引用 Ranges 而不指定 wb/ws
  • 我参考底部的StrMsg
  • 这不会启动消息框,这只是您使用字符串填充的变量。您可以在消息框中使用此字符串。
  • 正如@TimStack 所指出的,您在此行上方有语法错误。您的代码将永远不会到达StrMsg 而不会给您一个错误。修复上面的错误。然后使用MsgBox StrMsg

标签: vba loops case msgbox


【解决方案1】:

我已经使用您预先构建的字符串添加了启动消息框的行。

要了解有关消息框及其使用方法的更多信息,请查看this MDOCS page

With ThisWorkbook.Sheets("Form")
    If Len(.Range("B2")) = 0 Then
        .Range("B2") = InputBox("Enter your name:  ")
    ElseIf Len(.Range("B3")) = 0 Then
        .Range("B3") = InputBox("Please enter your email: ")
    ElseIf Len(.Range("B4")) = 0 Then
        .Range("B4") = InputBox("Please Enter Chocolate Amount:  ")
    ElseIf Len(.Range("B5")) = 0 Then
        Range("B5") = InputBox("Please Enter Vanilla Amount : ")
    ElseIf Len(.Range("B6")) = 0 Then
        Range("B6") = InputBox("Please Enter Strawberry Amount: ")
    Else
        TotalOrdered = .Range("B4").Value + .Range("B5").Value + .Range("B6").Value
        Exit Sub
    End If
End With

'goes through checking the order and number and discount amount
Select Case Price
    Case TotalOrdered >= 6 And TotalOrdered <= 10
        Price = TotalOrdered * OrderPrice * 0.95
    Case TotalOrdered >= 11 And TotalOrdered <= 20
        Price = TotalOrdered * OrderPrice * 0.9
    Case TotalOrdered >= 21
        Price = TotalOrdered * OrderPrice * 0.8
    Case Else 'less than 6
        Price = TotalOrdered * OrderPrice
       End Select

'I incorporated the unit price with discount so it is more informative for the customer
StrMsg = ("Unit Price: $" & Price / TotalOrdered _ 'format function from HW 2
& "Quantity: " & TotalOrdered _
& "Tax Rate: $" & TaxRate _
& "Final Total Price: " & Price * TaxRateMultiplier)

MsgBox strMsg, vbInformation, "Prices"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2018-06-22
    相关资源
    最近更新 更多