dbTalk Databases Forums  

Access to Excel Automation Changes Values of Decimals

comp.databases.ms-access comp.databases.ms-access


Discuss Access to Excel Automation Changes Values of Decimals in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
BB
 
Posts: n/a

Default Access to Excel Automation Changes Values of Decimals - 05-11-2010 , 09:21 PM






I've got an Access 2007 application that uses automation to create an
Excel 2007 Workbook. Everything works great except that it changes
the value of longer decimals. If I send it a value of 0.0599, it
shows up in Excel as .0600. So it's rounding but keeping the correct
number of decimal places.

Here's the relevant code. Any idea what I'm doing wrong?

Private Sub SetCellValue(pobjWB As Excel.Workbook, psRange As String,
pvValue As Variant, Optional piDecimals As Integer = 2)

Dim sDecimals As String

With pobjWB.ActiveSheet
With .Range(psRange)

sDecimals = Space(0)
If Not IsMissing(piDecimals) Then
If piDecimals > 0 Then
sDecimals = "." & String(piDecimals, "0")
End If
End If

.NumberFormat = "0" & sDecimals

.Value = pvValue
End With
End With

End Sub

And the call would be something like:

SetCellValue objWB, "G3", 0.0599, 4

Reply With Quote
  #2  
Old   
Roger
 
Posts: n/a

Default Re: Access to Excel Automation Changes Values of Decimals - 05-12-2010 , 03:38 AM






On May 11, 8:21*pm, BB <billb... (AT) gmail (DOT) com> wrote:
Quote:
I've got an Access 2007 application that uses automation to create an
Excel 2007 Workbook. *Everything works great except that it changes
the value of longer decimals. *If I send it a value of 0.0599, it
shows up in Excel as .0600. *So it's rounding but keeping the correct
number of decimal places.

Here's the relevant code. *Any idea what I'm doing wrong?

Private Sub SetCellValue(pobjWB As Excel.Workbook, psRange As String,
pvValue As Variant, Optional piDecimals As Integer = 2)

* * Dim sDecimals As String

* * With pobjWB.ActiveSheet
* * * * With .Range(psRange)

* * * * * * * * sDecimals = Space(0)
* * * * * * * * If Not IsMissing(piDecimals) Then
* * * * * * * * * * If piDecimals > 0 Then
* * * * * * * * * * * * sDecimals = "." & String(piDecimals, "0")
* * * * * * * * * * End If
* * * * * * * * End If

* * * * * * * * .NumberFormat = "0" & sDecimals

* * * * * * * * .Value = pvValue
* * * * End With
* * End With

End Sub

And the call would be something like:

SetCellValue objWB, "G3", 0.0599, 4
I tried this with access97 and it works fine

Reply With Quote
  #3  
Old   
BB
 
Posts: n/a

Default Re: Access to Excel Automation Changes Values of Decimals - 05-13-2010 , 10:46 AM



On May 12, 3:38*am, Roger <lesperan... (AT) natpro (DOT) com> wrote:
Quote:
I tried this with access97 and it works fine
Thanks Roger.

Is there anyone out there who can try it with Access 2007? It's
driving me crazy!

Reply With Quote
  #4  
Old   
BB
 
Posts: n/a

Default Re: Access to Excel Automation Changes Values of Decimals - 05-13-2010 , 10:46 AM



On May 12, 3:38*am, Roger <lesperan... (AT) natpro (DOT) com> wrote:
Quote:
I tried this with access97 and it works fine
Thanks Roger.

Is there anyone out there who can try it with Access 2007? It's
driving me crazy!

Reply With Quote
  #5  
Old   
Salad
 
Posts: n/a

Default Re: Access to Excel Automation Changes Values of Decimals - 05-13-2010 , 12:49 PM



BB wrote:

Quote:
On May 12, 3:38 am, Roger <lesperan... (AT) natpro (DOT) com> wrote:

I tried this with access97 and it works fine


Thanks Roger.

Is there anyone out there who can try it with Access 2007? It's
driving me crazy!
Maybe this will work. Then again, maybe not
x = 0.5999
? formatnumber(x,2)
0.60
? formatnumber(x,4) 'default leading 0
0.5999
? formatnumber(x,4,0) 'no leading 0. -1 to force leading 0
..5999

There are newsgroups related to Excel. Maybe try one of them like
microsoft.public.excel.worksheet.functions

Reply With Quote
  #6  
Old   
BB
 
Posts: n/a

Default Re: Access to Excel Automation Changes Values of Decimals - 05-13-2010 , 01:40 PM



On May 13, 12:49*pm, Salad <sa... (AT) oilandvinegar (DOT) com> wrote:
Quote:
BB wrote:
On May 12, 3:38 am, Roger <lesperan... (AT) natpro (DOT) com> wrote:

I tried this with access97 and it works fine

Thanks Roger.

Is there anyone out there who can try it with Access 2007? *It's
driving me crazy!

Maybe this will work. *Then again, maybe not
x = 0.5999
? formatnumber(x,2)
0.60
? formatnumber(x,4) * *'default leading 0
0.5999
? formatnumber(x,4,0) *'no leading 0. *-1 to force leading 0
.5999

There are newsgroups related to Excel. *Maybe try one of them like
microsoft.public.excel.worksheet.functions
Bingo! That did it! Using:

.NumberFormat = "$0.0000"
.Value = FormatNumber(pvValue, 4, vbFalse)

for the assignment did the trick. Even though FormatNumber tells it
not to use a leading zero, the .NumberFormat property on the previous
line puts in both a dollar symbol and the leading zero. Strange, but
true!

Thanks, Salad!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.