Return fractional part of values

G

Guest

In an internal VBA calculation, I need to return the fractional part of a
value in a cell.

For example,

5.35 returns 0.35

7.00 returns 0

I thought: dValue = ActiveCell.Value Mod 1

would do it, but this doesn't work...don't know why...

Thanks much in advance for your assistance.
 
N

Niek Otten

<doesn't work> What does that mean?

From Excel Help:

The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers)

Try:

Function test(a As Double) As Double
test = a - Int(a)
End Function

Use "Fix" instead of "Int" depending on what you require; see VBA Help

--
Kind regards,

Niek Otten
Microsoft MVP - Excel


| In an internal VBA calculation, I need to return the fractional part of a
| value in a cell.
|
| For example,
|
| 5.35 returns 0.35
|
| 7.00 returns 0
|
| I thought: dValue = ActiveCell.Value Mod 1
|
| would do it, but this doesn't work...don't know why...
|
| Thanks much in advance for your assistance.
 
J

JE McGimpsey

don't know why...

The operative sentence in XL/VBA Help ("Mod"):

The modulus, or remainder, operator divides number1 by number2
***(rounding floating-point numbers to integers)***

Instead use

With ActiveCell
dValue = .Value - Fix(.Value)
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top