Problem setting cell values in vba

L

Legacy 69493

Guest
Hi

Can somebody tell me what I am doing wrong? I use Excel 2002 german version.

I set cell values with code like this:

....cells(4,5).value = 12

In the Excel Sheet the value shows like 12.000000023242
How can I avoid that?

Thanks for your help!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Is there nothing else in the code that changes the value? Can we see the rest of your code please?

Thanks
Jon
 
Upvote 0
Hi

Is there nothing else in the code that changes the value? Can we see the rest of your code please?

Thanks
Jon

You're right! I am very new to vba ;) Minimal code is
Code:
Private Sub CommandButton1_Click()
   Dim a As Single
   a = 4 * 3.12
   Application.ActiveCell = a  'implicit typecast to double????
End Sub

The single value seems to be typecasted to double that causes the incurracy. In the cell the result is 12,4799995422363. My "solution" for the moment is : Application.ActiveCell = CDbl(CStr(a)) :oops:

So how to solve it better?
Thx!
 
Upvote 0
Hi,

It seems to me you want to round your value by declaring your variable as Single. "Single" though still allows for commas. You need the variable type "LONG". Check out VBAhelfiles for declaration types...

Dim a As Long

(you could use Integer, but anyway VBA will use the same amount of memory as Long)

kind regards,
Erik
 
Upvote 0
Hi

Hi,

It seems to me you want to round your value by declaring your variable as Single. "Single" though still allows for commas. You need the variable type "LONG". Check out VBAhelfiles for declaration types...

No. my problem is that I have some values in variables of type single. For some reason I need these values in variables of type double. According to microsoft help CDbl(expression) returns a double value from the expression.

See code that illustrates my problem:

Code:
   Dim a As Single
   Dim b As Double
   
   a = 4 * 3.12
   b = CDbl(a)

a contains 12,48 as expected
b contains 12,4799995422363 :rolleyes:

Why???? What's wrong with my code?

thx!
 
Upvote 0
Unfortunately, I don't have much to add. This isn't the first time I've encountered this kind of a problem and am at a loss as to why it happens.

Assigning the calculation to a double works, assigning it to a single works. But, assigning a single to a double leads to a rounding error. Maybe, if I laid out the number in binary both as a single and a double I'd understand why it happens...
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,338
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top