View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
joeu2004 joeu2004 is offline
external usenet poster
 
Posts: 2,059
Default Code Not Recognizing Decimal

"Ron" wrote:
If the column is out of balance .49 cents it does not activate
the MSGBOX but if it's out .50 cents then the MSGBOX is activated.


It does not matter whether you do SumRng<0 or SumRng<0.0.

But what is the type of SumRng?

I suspect it is Long or Integer. It should be Variant, Currency or Double.
(I prefer Double.)

FYI, I think it would be more prudent to compute:

SumRng = WorksheetFunction.Round(WorksheetFunction.Sum(Sele ction),2)

which could be written:

With WorksheetFunction
SumRng = .Round(.Sum(Selection),2)
End With

That ameliorates anomalies that arise due to the internal binary arithmetic,
which often causes small decimal fraction differences from what you see when
a cell is formatted with 2 decimal places.

Of course, the use of ROUND() will also mask more significant differences as
well. If that is a concern, then compute SumRng as you did, but do a
comparison like:

If Abs(SumRng) < 0.00001 Then

selecting an appropriate value for 0.00001 that satisfies your degree of
tolerance.

BTW, I use WorksheetFunction.Round (Excel ROUND) instead of VBA Round()
because the latter rounds differently than Excel does. See the description
of the CInt and CLng functions for details.


----- original message -----

"Ron" wrote in message
...
Hello all,

Anyone have any ideas why this code does not recognize the decimal.
I'm trying to test a column to make sure it equals zero. However for
some reason this code does not recognize the trailing decimals. When
I change IF SUMRNG < 0 then to IF SUMRNG < 0.00 VBA alters it to
0#. If the column is out of balance .49 cents it does not activate
the MSGBOX but if it's out .50 cents then the MSGBOX is activated.
Appreciate your assistance, Ron

With Selection
SumRng = Application.WorksheetFunction.Sum(Selection)
If SumRng < 0 Then
MsgBox "Out of Balance, Please review and make the necessary
corrections. Balance should equal ZERO. " & SumRng

End If
End With