Question

This is probably an easy one, but I've got this Excel vba macro where I'm trying to divide two integers and I end up with a round number, even though there should be a comma in there. For example 278 / 101 = 3. While it's really 2,75 and not 3. Why is this?

The macro is really simple, like this:

Dim intFull, intLow , intDivided as Integer
intFull = 278
intLow = 101
intDivided = intFull \ intLow
Was it helpful?

Solution

Your result variables is an integer

If you work with doubles instead you will get 2.752 etc - which can be rounded using dbDivided = Round(lngFull / lngLow, 2)

[variables updated to be more meaningful]

Sub redone()
    Dim lngFull As Long
    Dim lngLow As Long
    Dim dbDivided As Double
    lngFull = 278
    lngLow = 101
    dbDivided = Round(lngFull / lngLow, 2)
End Sub

OTHER TIPS

Sure you used a forward slash and not a backslash? (See also: http://zo-d.com/blog/archives/programming/vba-integer-division-and-mod.html)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top