Search code examples
vbasolidworks

VBA Solidworks, Round Up


I am trying to make a programme in vba to construct an axle in solidworks, this has to be done by entering the the forces it is exposed to in a userform. My formula works just fine, but I would like it to round up all the values to a whole number, because making a 35,65 mm diameter axle is just silly.

I know I can do use "Round(Diameter, 0)" for this, but I don't want it to round down because this would make it weaker.

Short: How does one round up a number to a whole number in VBA?

For example: 7,3 => 8

Any input would be greatly appreciated!

Thank you

Rob


Solution

  • VBA's True is mathematically equal to -1 and False to 0 so this should do it for you.

    Dim axl as double
    axl = 36.5
    axl = int(axl) - (int(axl) <> axl)
    debug.print axl
    

    That assume that in a physical world, an axle's diameter cannot have a negative dimension. IOW, that formula rounds away from zero when axl is positive but rounds toward zero when axl is negative.

    One last caveat; use exactly as provided and do not change Int(axl) to CInt(axl). CInt rounds the value to the nearest integer like Round(axl, 0) while Int strips off any decimal portion.