PC Review


Reply
Thread Tools Rate Thread

Decimal problem

 
 
Gustaf
Guest
Posts: n/a
 
      3rd Jun 2009
In a VBA project I'm supporting, I ran into a line where a cell is assigned the value

"=RC[4] / (" + Format(100 - dblNewProdSaving) + " / 100)"

I get a run-time error on this line. However, if I manually change the cell from where dblNewProdSaving is taken, from 0,02 to 0.02, it runs. Why is this and what can I do about it?

Gustaf
 
Reply With Quote
 
 
 
 
Patrick Molloy
Guest
Posts: n/a
 
      3rd Jun 2009
syntax for format is wrong

"=RC[4] / (" + Format(100 - dblNewProdSaving ,"0.00" ) + " / 100)"

but i suggest

with rangetarget
.formulaR1C1 = "=RC[4] / (" & (100 - dblNewProdSaving ) & ")"
.NumberFormat ="0.00%"
end with

format the result generally overcomes issues with results that can often
unexpectedly occur when an interim calculation is formatted , eg rounding
issue


"Gustaf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In a VBA project I'm supporting, I ran into a line where a cell is
> assigned the value
>
> "=RC[4] / (" + Format(100 - dblNewProdSaving) + " / 100)"
>
> I get a run-time error on this line. However, if I manually change the
> cell from where dblNewProdSaving is taken, from 0,02 to 0.02, it runs. Why
> is this and what can I do about it?
> Gustaf


 
Reply With Quote
 
Gustaf
Guest
Posts: n/a
 
      3rd Jun 2009
Patrick Molloy wrote:

> syntax for format is wrong
>
> "=RC[4] / (" + Format(100 - dblNewProdSaving ,"0.00" ) + " / 100)"


That's what I thought too, but I get the same error. By the way, am I right that VBA always expects "." as decimal sign, regardless of my regional settings?

> but i suggest
>
> with rangetarget
> .formulaR1C1 = "=RC[4] / (" & (100 - dblNewProdSaving ) & ")"
> .NumberFormat ="0.00%"
> end with


Sorry to say, but this also produces the same error: "Application defined or object defined error" (duh!). Also tried putting NumberFormat first.

As an alternative, I tried changing the format of the cell from which I take dblNewProdSaving:

dblNewProdSaving = Format(ws.Cells(5, 4), "0.00%")

But this results in a "type mismatch" error. Ideally, I'd like to change it behind the scenes, to avoid confusing the user.

Gustaf
 
Reply With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      3rd Jun 2009
what's the value in dblNewProdSaving when you step through?

"Gustaf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Patrick Molloy wrote:
>
>> syntax for format is wrong
>>
>> "=RC[4] / (" + Format(100 - dblNewProdSaving ,"0.00" ) + " / 100)"

>
> That's what I thought too, but I get the same error. By the way, am I
> right that VBA always expects "." as decimal sign, regardless of my
> regional settings?
>
>> but i suggest
>>
>> with rangetarget
>> .formulaR1C1 = "=RC[4] / (" & (100 - dblNewProdSaving ) & ")"
>> .NumberFormat ="0.00%"
>> end with

>
> Sorry to say, but this also produces the same error: "Application defined
> or object defined error" (duh!). Also tried putting NumberFormat first.
>
> As an alternative, I tried changing the format of the cell from which I
> take dblNewProdSaving:
>
> dblNewProdSaving = Format(ws.Cells(5, 4), "0.00%")
>
> But this results in a "type mismatch" error. Ideally, I'd like to change
> it behind the scenes, to avoid confusing the user.
>
> Gustaf


 
Reply With Quote
 
Gustaf
Guest
Posts: n/a
 
      3rd Jun 2009
Patrick Molloy wrote:

> what's the value in dblNewProdSaving when you step through?


The value is 0,02 in the cell and still 0,02 when transfered to dblNewProdSaving. If I change the cell to 0.02 the value of dblNewProdSaving becomes 2 when I step through. Looks odd but probably makes sense, because the variable is a percentage.

Gustaf
 
Reply With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      3rd Jun 2009
that makes a pretty big difference!

so
"=RC[4] / (" & (100 - dblNewProdSaving ) & ")"

should be

"=RC[4] / (" & (1 - dblNewProdSaving ) & ")"


"Gustaf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Patrick Molloy wrote:
>
>> what's the value in dblNewProdSaving when you step through?

>
> The value is 0,02 in the cell and still 0,02 when transfered to
> dblNewProdSaving. If I change the cell to 0.02 the value of
> dblNewProdSaving becomes 2 when I step through. Looks odd but probably
> makes sense, because the variable is a percentage.
>
> Gustaf


 
Reply With Quote
 
jaf
Guest
Posts: n/a
 
      3rd Jun 2009
"By the way, am I right that VBA always expects "." as decimal sign, regardless of my regional settings?"
> Gustaf


No. It's governed by regional settings.

John
 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      3rd Jun 2009
>"By the way, am I right that VBA always expects "." as decimal sign, regardless of my regional settings?"
>> Gustaf

>
>No. It's governed by regional settings.


But the regional settings can be overridden in Excel.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Wed, 3 Jun 2009 12:09:39 -0400, "jaf" <(E-Mail Removed)> wrote:

>"By the way, am I right that VBA always expects "." as decimal sign, regardless of my regional settings?"
>> Gustaf

>
>No. It's governed by regional settings.
>
>John

 
Reply With Quote
 
jaf
Guest
Posts: n/a
 
      3rd Jun 2009
Hi Chip,
Yes, I should have pointed that out also.
Tools>options>international "system separators"

John


"Chip Pearson" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> >"By the way, am I right that VBA always expects "." as decimal sign, regardless of my regional settings?"
>>> Gustaf

>>
>>No. It's governed by regional settings.

>
> But the regional settings can be overridden in Excel.
>
> Cordially,
> Chip Pearson
> Microsoft Most Valuable Professional
> Excel Product Group, 1998 - 2009
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email on web site)
>
>
> On Wed, 3 Jun 2009 12:09:39 -0400, "jaf" <(E-Mail Removed)> wrote:
>
>>"By the way, am I right that VBA always expects "." as decimal sign, regardless of my regional settings?"
>>> Gustaf

>>
>>No. It's governed by regional settings.
>>
>>John

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text box formatted to General Number with 2 decimal places NOTallowing the decimal place. p-rat Microsoft Access Form Coding 3 14th Jan 2008 05:20 PM
Make decimal work properly in fixed decimal entry in Excel 2003. =?Utf-8?B?am9obmdpaWk=?= Microsoft Excel Crashes 0 12th Apr 2006 06:05 AM
Converting 2-place decimal value to floating point decimal number with leading zero Kermit Piper Microsoft Excel Misc 3 18th Mar 2006 06:20 PM
Decimal Problem DS Microsoft Access Form Coding 2 9th Mar 2006 04:14 PM
Conversion problem SqlDBType.Decimal -> T-SqlType Decimal (12,6) P@rick Microsoft ADO .NET 2 28th May 2004 02:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:31 PM.