Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Variable Defined as Double is entered in cell with only 2 decimal places

All,

I'm having trouble trying to debug why variables defined as double and
used in calculations appear with only 2 decimal places when placed in
the cell. Here is some of the code.

..
..
..
Dim dblOpEx, dblCurrent, dblAddOnExp, dblPctInc As Double
Dim iBY, iAY, i, j As Double

iAY = ActiveSheet.Range("ad536")
iBY = ActiveSheet.Range("ad537")
dblOpEx = ActiveSheet.Range("ad538")
dblPctInc = ActiveSheet.Range("ad539")
dblCurrent = ActiveSheet.Range("ad540")
dblAddOnExp = ActiveSheet.Range("ad542")

For j = 0 To iRowCount + iEmptyCount - 1

If j < iEmptyCount Then
ActiveCell.Offset(j, 1).Value = 0

Else

If iBY Year(ActiveCell.Offset(j, -6)) Then
ActiveCell.Offset(j, 1).Value = dblCurrent
Else
ActiveCell.Offset(j, 1).Value = (dblCurrent +
dblAddOnExp) * (1 + dblPctInc) ^ (Year(ActiveCell.Offset(j, -6)) -
iAY)
End If

End If

Next j

The cell value of dblcurrent is 9.025004, but when it enters it in
cells BI704 thru BI717 it rounds it to 9.03 and this causes
calculation errors. Any idea why this is happening?

Any thoughts would be appreciated.---AJ
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 829
Default Variable Defined as Double is entered in cell with only 2 decimal places

"AJ Master" wrote:
I'm having trouble trying to debug why variables defined
as double and used in calculations appear with only 2
decimal places when placed in the cell. Here is some of
the code.

[....]
If iBY Year(ActiveCell.Offset(j, -6)) Then
ActiveCell.Offset(j, 1).Value = dblCurrent

[....]
The cell value of dblcurrent is 9.025004, but when it
enters it in cells BI704 thru BI717 it rounds it to
9.03 and this causes calculation errors. Any idea why
this is happening?


I suspect that the cells are formatted with 2 decimal places __and__ the
"Precision as displayed" (PAD) calculation option is set.

As a work-around, if not a solution, try the following:

ActiveCell.Offset(j, 1).NumberFormat = "General"
ActiveCell.Offset(j, 1) = dblCurrent

Better still, disable PAD, and use explicit rounding wherever appropriate.
I usually deprecate the use of PAD for the very problem (among others) that
you encountered: its effect is too pervasive.

For future note, when you have a problem that specific, it is prudent to
reduce the code to the minimum necessary to demonstrate the problem. It
should have been sufficient to write:

Sub testit()
Dim dblVal As Double
dblVal = 9.025004
Cells(1, 1) = dblVal
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 151
Default Variable Defined as Double is entered in cell with only 2 decimalplaces

On 3/08/2012 9:23 AM, AJ Master wrote:
All,

I'm having trouble trying to debug why variables defined as double and
used in calculations appear with only 2 decimal places when placed in
the cell. Here is some of the code.

.
.
.
Dim dblOpEx, dblCurrent, dblAddOnExp, dblPctInc As Double
Dim iBY, iAY, i, j As Double

iAY = ActiveSheet.Range("ad536")
iBY = ActiveSheet.Range("ad537")
dblOpEx = ActiveSheet.Range("ad538")
dblPctInc = ActiveSheet.Range("ad539")
dblCurrent = ActiveSheet.Range("ad540")
dblAddOnExp = ActiveSheet.Range("ad542")

For j = 0 To iRowCount + iEmptyCount - 1

If j < iEmptyCount Then
ActiveCell.Offset(j, 1).Value = 0

Else

If iBY Year(ActiveCell.Offset(j, -6)) Then
ActiveCell.Offset(j, 1).Value = dblCurrent
Else
ActiveCell.Offset(j, 1).Value = (dblCurrent +
dblAddOnExp) * (1 + dblPctInc) ^ (Year(ActiveCell.Offset(j, -6)) -
iAY)
End If

End If

Next j

The cell value of dblcurrent is 9.025004, but when it enters it in
cells BI704 thru BI717 it rounds it to 9.03 and this causes
calculation errors. Any idea why this is happening?

Any thoughts would be appreciated.---AJ


Hi

You could try setting the format at the same time as setting the value.

eg

If iBY Year(ActiveCell.Offset(j, -6)) Then
with ActiveCell
.Offset(j, 1).Value = dblCurrent
.Offset(j, 1).NumberFormat = "0.000000"
End with

Set the number of decimal places to whatever suits your need.

HTH
Mick










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
appended a cell, but need to keep decimal places [email protected] Excel Discussion (Misc queries) 2 April 11th 07 10:31 PM
integers entered are converted into decimal places e.g. 1 = 0.01 Alan H Excel Discussion (Misc queries) 1 February 19th 07 09:38 PM
Round variable to 4 decimal places achidsey Excel Programming 4 September 25th 05 02:43 PM
Entering numbers with variable decimal places. Jack Excel Worksheet Functions 8 February 2nd 05 04:35 AM
variable as Currency two decimal places Max Bialystock Excel Programming 2 May 10th 04 10:48 PM


All times are GMT +1. The time now is 04:11 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"