Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Excel dropping decimal places

I have a weird problem when trying to grab numbers with more than 4
decimal places.

For example if A1 = 123.123456

If I try to debug.print or use Range("A1") in a calculation, it only
shows: 123.1234

If I directly enter a cell on the spreadsheet like make cell B1 equal
to "=A1", the decimal places stay intact on the spreadsheet. I just
can't figure out how to get all the decimal places into a variable so I
can make a calculation with it.

Any ideas why Excel is dropping the extra decimals when I'm trying to
get the value as a Range?!!

Thanks.

John

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Excel dropping decimal places

Are you, by any chance, declaring your variable as the currency type?
Try changing it to a double:

Dim val1 As Currency
val1 = Range("A1").Value
Debug.Print val1
~123.1235

Dim val2 As Double
val2 = Range("A1").Value
Debug.Print val2
~123.123456

Ken Puls, CMA - Microsoft MVP (Excel)
www.excelguru.ca

wrote:
I have a weird problem when trying to grab numbers with more than 4
decimal places.

For example if A1 = 123.123456

If I try to debug.print or use Range("A1") in a calculation, it only
shows: 123.1234

If I directly enter a cell on the spreadsheet like make cell B1 equal
to "=A1", the decimal places stay intact on the spreadsheet. I just
can't figure out how to get all the decimal places into a variable so I
can make a calculation with it.

Any ideas why Excel is dropping the extra decimals when I'm trying to
get the value as a Range?!!

Thanks.

John

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Excel dropping decimal places

No. In fact, I can go to the immediate windows and just:

? range("A1")

and Excel drops the decimals.

It seems like the source cell formatting may be part of the cause. I
have A1 formatted with:
$#,##0;$ "("#,##0")"

If I do a general format, Excel doesn't drop the decimal.

This is a really weird problem. Maybe an Excel bug?!

Why would formatting affect the value Excel returns?

Ideas?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Excel dropping decimal places

Try
?range("a1").value2

But that's close to the suggestion that Ken proffered.

" wrote:

No. In fact, I can go to the immediate windows and just:

? range("A1")

and Excel drops the decimals.

It seems like the source cell formatting may be part of the cause. I
have A1 formatted with:
$#,##0;$ "("#,##0")"

If I do a general format, Excel doesn't drop the decimal.

This is a really weird problem. Maybe an Excel bug?!

Why would formatting affect the value Excel returns?

Ideas?


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Excel dropping decimal places

You're dead on, Dave. Laying on robotman's mask caused the issue for
me, but switching to the Value2 cleared it up. Still need to make it a
double, though. :)

With the numberformat applied:
Dim val1 As Currency
val1 = Range("A1").Value
Debug.Print val1
~123.1235

Dim val2 As Double
val2 = Range("A1").Value
Debug.Print val2
~123.1235

And this time with Value2:
Dim val1 As Currency
val1 = Range("A1").Value2
~123.1235

Dim val2 As Double
val2 = Range("A1").Value2
Debug.Print val2
~123.123456

So, without setting it to a variable first:
? cdbl(range("A1").Value2)

Cheers,

Ken Puls, CMA - Microsoft MVP (Excel)
www.excelguru.ca

Dave Peterson wrote:
Try
?range("a1").value2

But that's close to the suggestion that Ken proffered.

" wrote:
No. In fact, I can go to the immediate windows and just:

? range("A1")

and Excel drops the decimals.

It seems like the source cell formatting may be part of the cause. I
have A1 formatted with:
$#,##0;$ "("#,##0")"

If I do a general format, Excel doesn't drop the decimal.

This is a really weird problem. Maybe an Excel bug?!

Why would formatting affect the value Excel returns?

Ideas?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Excel dropping decimal places

Why would formatting affect the value Excel returns?

Currency format could indeed limit the number of dp returned to the Value
property. As Dave suggested use Value2 to return the full value.

Assuming a currency cell format try -

MsgBox VarType(ActiveCell.Value) = vbCurrency
MsgBox VarType(ActiveCell.Value2) = vbDouble

Regards,
Peter T

wrote in message
ups.com...
No. In fact, I can go to the immediate windows and just:

? range("A1")

and Excel drops the decimals.

It seems like the source cell formatting may be part of the cause. I
have A1 formatted with:
$#,##0;$ "("#,##0")"

If I do a general format, Excel doesn't drop the decimal.

This is a really weird problem. Maybe an Excel bug?!

Why would formatting affect the value Excel returns?

Ideas?



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default Excel dropping decimal places

I didn't even know there was a Value2. Interesting.

I still don't get why the cell format changes the value that Excel
returns. Isn't the format just a MASK? When directly referencing the
cell (not using any variable), I don't see why a mask changes the
value.

Again.. with no variables involved:

? Range("A1")

You lose dp when A1 is formatted as currency. But you are correct that

? Range("A1").Value2

returns all the dp.

Just trying to understand the relationship between cell format and
returned values.

Thanks for your insight!

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Excel dropping decimal places

Currency and dates have different .value2 from .value

If you look at VBA's help, you'll see a description of when you can expect this
behavior--it's not quite an explanation (to me), but it does describe it.

" wrote:

I didn't even know there was a Value2. Interesting.

I still don't get why the cell format changes the value that Excel
returns. Isn't the format just a MASK? When directly referencing the
cell (not using any variable), I don't see why a mask changes the
value.

Again.. with no variables involved:

? Range("A1")

You lose dp when A1 is formatted as currency. But you are correct that

? Range("A1").Value2

returns all the dp.

Just trying to understand the relationship between cell format and
returned values.

Thanks for your insight!


--

Dave Peterson
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
Fixed decimal places in Excel Garben Excel Discussion (Misc queries) 4 December 9th 08 09:49 PM
Dropping decimal places whern transfering to AS400? Transfer to I-Series proglem Excel Worksheet Functions 2 July 9th 08 11:40 PM
Subtracting two 2-decimal place numbers gives result 13-decimal places? [email protected] Excel Worksheet Functions 5 March 12th 07 10:38 PM
FIXED 2 DECIMAL PLACES, MUST ENTER ALL ZEROES AFTER DECIMAL POINT. SUKYKITTY Excel Discussion (Misc queries) 3 July 6th 05 01:50 PM
Decimal places in Excel 2003 EXCELNCBOY Excel Worksheet Functions 6 November 5th 04 03:39 PM


All times are GMT +1. The time now is 03:40 AM.

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

About Us

"It's about Microsoft Excel"