• Calculations in VB (Word 97/VBA)

    Author
    Topic
    #374669

    I have a project that I thought was working fine. I had declared the variables as integers. In one text box, the user will type a number. The macro then checks to see if the number is higher than 3. If it is, it subtracts 3 from that number and creates a variable called “IndExtra”. Sounds simple enough. However, the macro works fine if the number is 1-9 or anything higher than 29….but won’t calculate anything from 10-29 inclusive, this is extremely puzzling.

    I have declared an variable called “Individual” as an integer.
    Individual = frmPTO1390.TextBox2.Text
    I have even tried:
    Individual = Int(frmPto1390.TextBox2.Text)

    I then have a statement similar to:
    If Individual > 3 Then
    do this routine
    End If

    What am I doing wrong here??? As always, thanks for your excellent advice.

    Viewing 1 reply thread
    Author
    Replies
    • #606813

      Just a very quick guess (and I mean guess). What happens if you change .text to .value?

    • #606885

      You’re going to have to tell us what you mean by it not calculating values in that range. Do you get an error message or what happens? I would suspect the logic you’re using in your macro if it isn’t calculating that range properly. Something like this should work:

      Dim Individual as Integer
      Dim IndExtra as Integer

      Individual = Val(frmPT01390.TextBox2.Text)
      If Individual > 3 Then
      IndExtra = Individual – 3
      End If

      You could also use CInt instead of Val since you’re working with an integer value anyhow. If it doesn’t work, then the problem is the way you’re referencing the textbox. I assumed here that your reference to the form name was correct.

      • #607018

        Bless you Charlotte! What a simple thing…when I changed Int to Val — it worked perfectly. This novice very much appreciates your assistance!

        • #607208

          The following nuggets are gleaned from “VB&VBA In a Nutshell” (O’Reilly) – in case they are useful:

          CInt rounds any fractional portion of the number.
          Int discards any fractional portion.

          CInt casts the number passed to it as an Integer.
          Int returns the same data type that was passed to it.

          Gary

    Viewing 1 reply thread
    Reply To: Reply #607018 in Calculations in VB (Word 97/VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel