Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default if more than 2 decimal places

How can I determine programmatically within VBA whether or not the contents
of a cell have more than 2 decimal places? Anyone able to help?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default if more than 2 decimal places

Do you mean two places displayed in the cell, or two places in the
underlying value?

Cliff Edwards

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,345
Default if more than 2 decimal places

One way:

If Int(Cells(1, 1) * 100) / 100 = Cells(1, 1) Then
MsgBox "Less"
Else
MsgBox "More"
End If

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"gbink" wrote in message
...
How can I determine programmatically within VBA whether or not the
contents
of a cell have more than 2 decimal places? Anyone able to help?



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default if more than 2 decimal places

Thanks for responding Cliff

The cell is set to display as 2 dp but the underlying value may have more
decimal places. I want to check if the underlying value has more than 2 dp.

I have trying to code something like

if cell.value < int(cell.value *100) / 100

and this does work on most occasions but sometimes I get a unexpected result.

g

"ward376" wrote:

Do you mean two places displayed in the cell, or two places in the
underlying value?

Cliff Edwards


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default if more than 2 decimal places

Thanks Sandy.

Your code is virtually identical to mine however I am finding the sometimes
the INT function is not returning expected results. e.g the cell contains
4.77 yet when I run the code I get a 'Less than 2dp' result.

when i put a watch in the code and examine INT(cell.value * 100) / 100 I get
4.76

G

"Sandy Mann" wrote:

One way:

If Int(Cells(1, 1) * 100) / 100 = Cells(1, 1) Then
MsgBox "Less"
Else
MsgBox "More"
End If

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"gbink" wrote in message
...
How can I determine programmatically within VBA whether or not the
contents
of a cell have more than 2 decimal places? Anyone able to help?






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 449
Default if more than 2 decimal places

Rounding errors due to decimal-binary-decimal conversions may create
differences like 0.000000000012, so instead of equal, check the size of the
difference and allow a little slack.

Also, check what INT does to negative values.

HTH. Best wishes Harald

"gbink" wrote in message
...
Thanks Sandy.

Your code is virtually identical to mine however I am finding the
sometimes
the INT function is not returning expected results. e.g the cell contains
4.77 yet when I run the code I get a 'Less than 2dp' result.

when i put a watch in the code and examine INT(cell.value * 100) / 100 I
get
4.76

G

"Sandy Mann" wrote:

One way:

If Int(Cells(1, 1) * 100) / 100 = Cells(1, 1) Then
MsgBox "Less"
Else
MsgBox "More"
End If

--
HTH

Sandy
In Perth, the ancient capital of Scotland
and the crowning place of kings


Replace @mailinator.com with @tiscali.co.uk


"gbink" wrote in message
...
How can I determine programmatically within VBA whether or not the
contents
of a cell have more than 2 decimal places? Anyone able to help?





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default if more than 2 decimal places

Give this a try...

If Len(CStr(Cells(1, 1).Value)) - 2 _
InStr(CStr(Cells(1, 1).Value), ".") Then
MsgBox "More"
Else
MsgBox "Equal or Less"
End If

Rick


"gbink" wrote in message
...
How can I determine programmatically within VBA whether or not the
contents
of a cell have more than 2 decimal places? Anyone able to help?


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default if more than 2 decimal places

Thanks Rick that did teh trick

"Rick Rothstein (MVP - VB)" wrote:

Give this a try...

If Len(CStr(Cells(1, 1).Value)) - 2 _
InStr(CStr(Cells(1, 1).Value), ".") Then
MsgBox "More"
Else
MsgBox "Equal or Less"
End If

Rick


"gbink" wrote in message
...
How can I determine programmatically within VBA whether or not the
contents
of a cell have more than 2 decimal places? Anyone able to help?



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal Places college student ATL Excel Worksheet Functions 4 September 13th 07 02:12 AM
Subtracting two 2-decimal place numbers gives result 13-decimal places? [email protected] Excel Worksheet Functions 5 March 12th 07 10:38 PM
Decimal Places Rab Swinney Excel Discussion (Misc queries) 5 January 10th 07 07:01 PM
Decimal Places Sundara Murthy Excel Programming 1 November 20th 06 10:27 AM
FIXED 2 DECIMAL PLACES, MUST ENTER ALL ZEROES AFTER DECIMAL POINT. SUKYKITTY Excel Discussion (Misc queries) 3 July 6th 05 01:50 PM


All times are GMT +1. The time now is 10:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"