joacal
01-09-2009, 07:12 AM
OFFICE ENTERPRISE 2007
Sub BuyBooks()
Dim bookcode, k, i, bookQty As Integer
Dim Booktitle As String
Dim bookprice, totalprice As Currency
For k = 1 To 5 Step 1
MsgBox "Book code " & Cells(k + 2, 2) & ", Book Title is " & Cells(k + 2, 3)
Next k
bookQty = InputBox("Input book quantity: ")
bookcode = InputBox("Enter book code: ")
For i = 1 To 5 Step 1
If bookcode = i Then
bookprice = Val(Cells(i + 2, 4))
Booktitle = Cells(i + 2, 3)
totalprice = bookQty * bookprice
Else
MsgBox "No such book."
Exit For
End If
MsgBox "Book Quantity is " & bookQty & ", Book title is " & Booktitle & ", Total Price is $" & totalprice
Next i
MsgBox "Program End"
End Sub
I'm wondering why is it that it doesnt fall through the If statement when i input 1 as bookcode? I believe my spreadsheet has nothing wrong.. I always get the "no such book" statement.
Edit Lucas: vba tags added to code.
Sub BuyBooks()
Dim bookcode, k, i, bookQty As Integer
Dim Booktitle As String
Dim bookprice, totalprice As Currency
For k = 1 To 5 Step 1
MsgBox "Book code " & Cells(k + 2, 2) & ", Book Title is " & Cells(k + 2, 3)
Next k
bookQty = InputBox("Input book quantity: ")
bookcode = InputBox("Enter book code: ")
For i = 1 To 5 Step 1
If bookcode = i Then
bookprice = Val(Cells(i + 2, 4))
Booktitle = Cells(i + 2, 3)
totalprice = bookQty * bookprice
Else
MsgBox "No such book."
Exit For
End If
MsgBox "Book Quantity is " & bookQty & ", Book title is " & Booktitle & ", Total Price is $" & totalprice
Next i
MsgBox "Program End"
End Sub
I'm wondering why is it that it doesnt fall through the If statement when i input 1 as bookcode? I believe my spreadsheet has nothing wrong.. I always get the "no such book" statement.
Edit Lucas: vba tags added to code.