PDA

View Full Version : [SOLVED:] Mismatch Error



yojoe266
10-25-2017, 06:51 PM
Hi all, I'm very new to VBA and I am writing a subroutine that allows the user to enter a sales transaction with quantity and price with all prices having to end in .98 or .48. Then the sub will calculate the total for the transaction and gives them a 12% discount if the transaction totals more than $1000. A number of message boxes then appear to inform the user a couple things regarding the transaction. I keep getting this mismatch error when I run my code and I asked a friend who said to use the RIGHT function to help with the .98/.48 requirement but I'm not too sure how to implement that. I made a comment regarding which line im having trouble on and I have also attached a copy of my code. Any help would be appreciated! Thanks!



Sub SalesTransaction()


Dim NumberInput2 As Double
Dim QuantityInput As Integer
Dim Y As Double
Dim X As Double
Dim Z As Double
Dim A As Double
Dim NumberError2 As Boolean: NumberError2 = True
Dim QuantityError As Boolean: QuantityError = True
NumberInput2 = InputBox("Please Enter a price that end in in .98 or .48")
QuantityInput = InputBox("Please enter the quantity purchased")


If IsNumeric(NumberInput2) Then 'Test for number
If NumberInput2 Like "*.98" Or "*.48" Then 'Test for .98 and .48 <-keep getting mismatch error on this line
NumberError2 = False
End If
End If


If IsNumeric(QuantityInput) Then 'Test for number
If Not QuantityInput Like "*.*" Then 'Test for decimal
If QuantityInput > -1 Then 'Test for negative
QuantityError = False
End If
End If
End If


If NumberError2 Or QuantityError Then
MsgBox "Sorry, this is not a valid input"
Else
X = NumberInput2 * QuantityInput
Y = (NumberInput2 * QuantityInput) * 0.12
If X > 1000 Then 'discount for over 1000
Z = X - Y 'Calculating discount
Else
Z = X
End If
MsgBox ("The total amount of Sales is" & X) & vbCrLf & ("based on the user input.") 'msg box displaying total amount of sales
MsgBox ("A Discount of " & Y) & vbCrLf & ("is applied based on the total dollar amount of sales.") 'msg box displaying the discount applied
MsgBox ("The total sale amount with discount of " & Y) & vbCrLf & ("is applied based on the Total Sales and discount applied.") 'msg box displaying


End If


End Sub

SamT
10-26-2017, 08:05 AM
If Right(CStr(NumberInput2), 2) = "98" Or Right(CStr(NumberInput2), 2) = "48" Then

Or

X = Right(CStr(NumberInput2), 2)
If X = "98" Or X ="49" Then

yojoe266
10-26-2017, 11:07 AM
Thank you so much!