Test for Decimal

  • Thread starter Thread starter Ken Hudson
  • Start date Start date
K

Ken Hudson

In a section of code I am using the MOD function to test for cases where a
given number is a multiple of 51.

If Range("A1") MOD 51 = 0 then
....more code

In cell A1 I have 203.52. The function is returning true because the 203.52
is rounded up to 204 as part of the MOD test. I don't want to apply code to
A1l and need to test for the decimal before I use the MOD function, I think.

If Range("A1") <> "decimal" then
If Range("A1") MOD 51 = 0 then
...more code

Is this the logic I need and how do I test for the decimal?

Thanks.
 
You can test your value like this...

If InStr(Range("A1") .Value, ".") = 0 Then
If Range("A1") MOD 51 = 0 then
...more code

Rick
 
I would take the integer value subtract from the original.

With Range("A1")
If .Value - Int(.Value) = 0 And .Value Mod 51 = 0 Then
'more code
End If
End With
 
Here is two methods. either search for a decimal point in the number or use
Mod and test if the remainder equals 0.



If InStr(Range("J21").Text, ".") = 0 Then

End If

If (Range("J21") Mod 10) = 0 Then

End If
 
Thanks, Nigel.
I believe the correct expression for your side of the pond is "that works a
treat."
 
While it is probably not the case with the OP, just thought I would point
out that your Int function call will fail if the contents of A1 is a
non-number. Also, you could save a couple of characters by testing the
..Value=Int(.Value) directly instead of subtracting them and seeing if that
difference was zero.

Rick
 
Good call Rick, I was worried about floating point numbers but I think
taking the Integer is not an issue. Non-numeric values of course may not be
an issue but the OP could add this check.

--

Regards,
Nigel
(e-mail address removed)
 

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

Back
Top