Rounding error???

O

oggy

Hi
I have have wrote the following code to get a value from a cell in one
spreadsheet and put it into another sheet.

The problem is the value is in money ie £25.68 but when the macro puts
the value in the other sheet the value is rounded up or dowm ie £26.00

The following code is a small element of a large macro, but show the
problem if processed.

Dim price As Long
price = ActiveCell.Value
Sheets("Sheet3").Select
Range("I21").Select
ActiveCell.Value = price
End Sub

Any ideas to resolve the problem would be gratfully recieved.

Thanks
 
A

Andrew Taylor

You've defined price as being "Long". This is an integral
type, so it will round to an integer when you assign
ActiveCell.Value to it. Try

Dim price as Double

instead.
 
R

Ron Rosenfeld

Hi
I have have wrote the following code to get a value from a cell in one
spreadsheet and put it into another sheet.

The problem is the value is in money ie £25.68 but when the macro puts
the value in the other sheet the value is rounded up or dowm ie £26.00

The following code is a small element of a large macro, but show the
problem if processed.

Dim price As Long
price = ActiveCell.Value
Sheets("Sheet3").Select
Range("I21").Select
ActiveCell.Value = price
End Sub

Any ideas to resolve the problem would be gratfully recieved.

Thanks

Dim price as Long

Long is a long integer value. If you don't want your result to round, Dim
price as Double.

Also, there is a lot of redundancy in your code. There is no need for all the
selects or the last ActiveCell

==========================
Dim price As Double
price = ActiveCell.Value
Sheets("Sheet3").Range("I21").Value = price
=============================

would do the same thing.

Or even:

======================================
Sheets("Sheet3").Range("I21").Value = ActiveCell.Value
==========================================


--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top