Consulting

Results 1 to 7 of 7

Thread: need help with coding

  1. #1
    VBAX Newbie
    Joined
    Jan 2009
    Posts
    3
    Location

    need help with coding

    OFFICE ENTERPRISE 2007

    [VBA]
    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
    [/VBA]
    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.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi joacal,
    Welcome to the forum.
    It would help if you could provide some dummy data so we can see what you are encountering......


    ps when posting code, select the code and hit the vba button to format it for the forum.

    You can attach your workbook by hitting post reply at the bottom left of the last post and then aften entering your message, scroll down and look for a button that says "manage attachments"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Mentor
    Joined
    Dec 2008
    Posts
    404
    Location
    And so?:[VBA]Sub BuyBooks()
    Dim bookcode As Long, k As Long, i As Long
    Dim bookQty As Integer
    Dim Booktitle As String
    Dim bookprice As Currency, totalprice As Currency
    Dim IsBookExist As Boolean

    For k = 1 To 5 Step 1
    MsgBox "Book code: " & Cells(k + 2, 2) & vbCr & _
    "Book Title is: " & Cells(k + 2, 3)
    Next k

    bookQty = InputBox("Input book quantity: ")
    bookcode = InputBox("Enter book code: ")
    IsBookExist = False

    For i = 1 To 5
    If bookcode = i Then
    IsBookExist = True
    Exit For
    End If
    Next i

    If IsBookExist Then
    bookprice = Val(Cells(i + 2, 4))
    Booktitle = Cells(i + 2, 3)
    totalprice = bookQty * bookprice
    MsgBox "Book Quantity is " & bookQty & vbCr & _
    "Book title is " & Booktitle & vbCr & _
    "Total Price is $" & totalprice
    Else
    MsgBox "No such book."
    End If

    MsgBox "Program End"
    End Sub[/VBA]
    When you declare a few variables on the same line, you use As Type for each variable. Otherwise only the last type is specified, the rest are Variant.

    Artik

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Is it because you are testing bookcode against i not 1?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Newbie
    Joined
    Jan 2009
    Posts
    3
    Location
    Thanks alot for the help. I manage to solve my problem.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    For the records, how did you solve it?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Newbie
    Joined
    Jan 2009
    Posts
    3
    Location
    Its the problem with my declarations..
    I should have Dim every single variable..

    or using Val(bookcode)
    I was able to compare as well

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •