MSGBOX在订单点击时无法正常工作

时间:2019-03-26 13:39:02

标签: vba loops case msgbox

我有一个订购按钮,它将确定产品的价格,然后向用户显示订购详细信息。但是,我的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

1 个答案:

答案 0 :(得分:0)

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

要了解有关消息框及其使用方法的更多信息,请查看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"