Results 1 to 3 of 3
  1. #1
    Silver Lounger
    Join Date
    Dec 2000
    Location
    California, USA
    Posts
    1,758
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 Decimal Places (XL97;SR2)

    I'm having a problem rounding a number down to two decimal places. The code I'm using is:

    Sub FixedNumber()
    For Each Cell_In_Loop In Selection
    FNumber = CInt(Cell_In_Loop)
    Cell_In_Loop.Value = FNumber
    Next
    End Sub

    The variable FNumber can be rounded up/down to a whole number but I can't seem to retain two decimal places. There are instances where a cell contains a number such as 12.8237986 ... I would like the end result to be 12.83

    XL has a roundup function ROUNDUP(12.8237986,2) but I can't translate this to vba code although it is listed in the functions available to vba code.

    Thanks,
    John

  2. #2
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: 2 Decimal Places (XL97;SR2)

    John, use the Round function:<pre>Option Explicit
    Sub R2()
    Dim c As Range
    For Each c In Selection
    c = Round(c.Value, 2)
    Next c
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: 2 Decimal Places (XL97;SR2)

    If you need to RoundUp with VBA you can use :<pre> For Each oCell In Selection
    oCell.Value = Application.WorksheetFunction. _
    RoundUp(oCell.Value, 2)
    Next</pre>

    VBA itself does not have a round up function but you can call the worksheet fumction as above.

    Andrew C

Posting Permissions

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