Consulting

Results 1 to 4 of 4

Thread: Rounding up question

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Rounding up question

    When I run the code below it rounds up to the next dollar. I forget what you have to do to make it exact. Ex. 1621.89 instead of 1622.

    Sub payout()
    Dim bval As Long
    Dim myrange As Variant
    For Each myrange In Range("b6:b16")
         If myrange <> "" Then
            bval = myrange.Value + bval
         End If
    Next myrange
    MsgBox "total is " & bval
    End Sub
    Peace of mind is found in some of the strangest places.

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi austenr,

    With bval being Dim'med as a Long, it will always be a whole number. If you Dim it as a Double instead, you'll get the exact value.
    You'll probably also want to do
    bval = Round(MyRange.Value, 2) + bval
    if your MyRange.Value is more than 2 decimal points.
    Matt

  3. #3
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Thanks. I knew it had to do with the Dim but I had not tried Double. Solved
    Peace of mind is found in some of the strangest places.

  4. #4
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    You could also use Single or Currency, depending on the values you may be using to enter them. Each will work, I'd say take a look in the VBA help at the limits of each to determine which is best for your use

Posting Permissions

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