Results 1 to 17 of 17

Thread: Solved: Round a number up in Word VBA

  1. #1
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location

    Solved: Round a number up in Word VBA

    Been searching this morning for a method to round a number up in word but to no avail. eg. 17.453 => 17.46, That is I need to round up to two decimals places everytime.

    Any help with this would be greatly appreciated,

    Thanks
    Andrew;?
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  2. #2
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    Youl could use
    [VBA]? Format(17.459+0.005,"###0.00")[/VBA]

    where the + 0.005 makes sure that all values are rounded up by raising each value above the magical .005 from where it's always rounded up. And don't worry if the value gets to 17.464, because this will be rounded down to 17.46, so you won't have a difference there.

    Very crude, I know. But it serves my needs.

    Daniel

  3. #3
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    that may be crude but its the best idea i've seen so far. I wish Word VBA would incorporate the ceiling function available in Excel, that would make life that much easier... I guess its too much to ask from microsoft. Thanks for your help. I'll incorporate it as soon as i get this documentation I'm now working on done (like maybe 2011 if i'm lucky).

    Thanks again
    Andrew;?
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  4. #4
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    You're welcome, and if you find a better solution, I'd be interested, too.

    Daniel

  5. #5
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,168
    Location
    How about Round(17.459, 2)?

  6. #6
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    In Word-VBA? At least in Word97 I can't find that one.

  7. #7
    VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,168
    Location
    It's in VBA.Math in Word 2000. It will not round up like requested unless
    [VBA]
    Round(17.459+0.005,2)
    [/VBA]
    as you showed. The only real difference is Format returns a string the Round returns a Double/Single. Which can be gotten around by
    [VBA]
    CDbl(Format(17.459+0.005,"###0.00"))
    Val(Format(17.459+0.005,"###0.00"))
    [/VBA]

  8. #8
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Yes VBA 6 does have a round function if it was there in 97...don't remember that..(have to check)

    Remember that the Round function we all know off in Excel has a different result as the VBA Round function. (So results may differ from expectation)

    More background: http://support.microsoft.com/default...b;en-us;194983

    But you can still make custom Round function deppending on you're needs.
    More backgound: http://support.microsoft.com/kb/196652/EN-US/

    Enjoy!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  9. #9
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    That borught me to an idea, but it just does not work as expected and I have no idea why:

    [VBA]Public Function RoundUp(val As Double, digits As Integer)
    Dim mult As Double, i%, res As Double

    mult = -1
    For i = 1 To digits
    mult = mult * 10
    Next i

    res = (Int(val * mult) / mult)

    RoundUp = res
    End Function
    [/VBA]

    This should make use of the fact, the Int for negative numbers always round away from the 0. But it just does not do what it should, example:
    ? roundup(17.323,3)
    17.324

    If I go through the calculation stepts it should be:
    17.323 * (-1000) = -17323
    Int(-17323) = -17323
    -17323 / (-1000) = 13.323

    But if I combine the first 2 steps I get:
    ? int(17.323*(-1000))
    -17324

    Where's the problem??

    By the way, this one should work correct:
    [VBA]Public Function RoundUp(val As Double, digits As Integer)
    Dim mult As Double, i%, res As Double

    mult = -1
    For i = 1 To digits
    mult = mult * 10
    Next i

    res = val * mult
    res = (Int(res) / mult)

    RoundUp = res
    End Function[/VBA]

    But I still would like to know where the difference above comes from.

  10. #10
    Knowledge Base Approver
    Space Cadet
    VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    So many interesting things to consider. Thank you all for your help and suggestions. Steiner I think your problem may be related to the way VBA evaluates the brackets in the equation. I can't be sure though but that would be my first guess.

    Thanks again to everyone
    Andrew;?
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Steiner,

    I think your problem comes from the inherent inaccuracies in floating point arithmetic and there isn't much you can do about it.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  12. #12
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    How about:[vba]Sub RoundUp()
    Dim M As Double, N As Double
    N = 3.1415926 '< example
    'use 10 for 1 dec place, 100 for 2 dec places, 1000 for 3
    '(NOTE: to round off {not up}, omit the "+ 0.5")
    M = Int(N) + ((100 * (N - Int(N)) + 0.5) \ 1) / 100
    End Sub[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  13. #13
    Knowledge Base Approver
    Space Cadet VBAX Tutor sandam's Avatar
    Joined
    Jan 2005
    Location
    London
    Posts
    292
    Location
    Thank you all for the great ideas.

    I still think Ceiling should be incorporated into all VBA versions, it does exactly what is needed . Anyways. I think this thread is quite solved so I'm going to mark it so.



    Andrew;?
    Nothing annoys a non-conformist more than other non-conformists who refuse to conform to the rules of non-conformity.


    Confused is my normal state of mind


  14. #14
    BoardCoder
    Licensed Coder VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    620
    Location
    A couple of generic rounding functions I use:

    [VBA]
    Function RoundDec(ByVal x As Double, Optional Direction As Integer = 0, Optional DecimalPlaces As Long = 0) As Double
    x = x * 10 ^ DecimalPlaces
    If Direction < 0 Then
    RoundDec = Int(x)
    ElseIf Direction > 0 Then
    If x = Int(x) Then
    RoundDec = Int(x)
    Else
    RoundDec = Int(x) + 1
    End If
    Else
    If x - Int(x) < 0.5 Then
    RoundDec = Int(x)
    Else
    RoundDec = Int(x) + 1
    End If
    End If
    RoundDec = RoundDec / 10 ^ DecimalPlaces
    End Function

    Function RoundSF(ExactValue As Double, SigFigs As Integer, Optional Direction As Integer = 0) As Double

    Dim x As Integer
    x = Int(VBA.Math.Log(ExactValue) / VBA.Math.Log(10)) + 1
    RoundSF = Int(RoundDec(ExactValue / 10 ^ (x - SigFigs), Direction)) * 10 ^ (x - SigFigs)

    End Function
    [/VBA]

    For rounding up, down, nearest to decimal places or significant figures.

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  15. #15
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Let's Round things up!

    One thing is not mentioned in this subject. There is also a simple way to achieve the same rounding accuracy as Excel has...USE EXCEL!

    A Simple function to take advantage off what's allready there:[VBA]
    Public Function ExcelRound(dValue As Double, dDec As Double) As Double
    Dim oExcel As Object
    Set oExcel = CreateObject("Excel.Application")

    With oExcel.WorksheetFunction
    ExcelRound = .Round(dValue, dDec)
    End With
    Set oExcel = Nothing
    End Function

    Sub TestDouble()
    MsgBox ExcelRound(10.543, 2)
    MsgBox ExcelRound(10.547, 2)
    End Sub
    [/VBA]
    Have fun testing all the options!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  16. #16
    BoardCoder
    Licensed Coder
    VBAX Expert mark007's Avatar
    Joined
    May 2004
    Location
    Leeds, UK
    Posts
    620
    Location
    Hi Mos,

    The problem with that approach is it will run very slow!

    "Computers are useless. They can only give you answers." - Pablo Picasso
    Mark Rowlinson FIA | The Code Net

  17. #17
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by mark007
    Hi Mos,

    The problem with that approach is it will run very slow!

    Hi Mark,

    True..but intention was to add to the diversity off the discussion.
    It still is a good possibility and it depends on what you wish to achieve if it will be slow or not...
    (not suitable for batch programming off course)

    See Yah!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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