Glitch with Currency format?

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,376
Hello all,

I have a problem with the currency format with 4 decimal places.

here is my macro.

Code:
Sub go()
Range("C5") = Range("C5") + Range("C4")
End Sub

as you can see it adds the conecnt of "R4" to what is in "R5"

If works fine when the calls are formatted as number with 4 decimal places.
example if R4 is 0.1111 and R5 is 0.2222 after running the macro R5 becomes 0.3333

But if the cells are formatted as currency and 4 decimal places the then excel returns $0.3300

Give it a try and see what you get.
Anyone else have the same problem?

Ross
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You could hold the ans in a Double variable before reapplying:

Code:
Sub goe()
Dim ans As Double: ans = Range("C5") + Range("C4"): Range("C5") = ans
End Sub

(note I wouldn't use "go" as a name for a subroutine!)
 
Upvote 0
Hi

Another option:

Code:
Sub goe()
Range("C5") = Range("C5").Value2 + Range("C4").Value2
End Sub
 
Upvote 0
Per Charles Williams' site (DecisionModels.com):

It is faster (15-20%) to use the Range.Value2 property rather than the (default) Range.Value property. The Range.Value property attempts to convert cells formatted as Dates to a variant containg a VBA date type, and cells formatted as currency to a variant containing a VBA Currency type. Range.value2 attempts to convert date and Currency formatted cells into Variants containing Doubles.

which suggests that the speed improvement will only be garnered if the range conatins Date or Currency formatted cells (so no improvements for other types).
 
Upvote 0
Thanks Richard. That makes sense - the help for Range2 said it was the same as Range except that it doesn't use the Currency and Date data types.
 
Upvote 0
Hi

We sometimes use libraries with code written in other languages. We can also access excel from other languages, other than vba.

I'd use Value2 with code written in other languages. Other languages may not have Currency and Date data types. The Double type, however, is usually available. Using Value2 in these cases, makes sure you'll always work with doubles, which are common to both languages. I think that this alone justifies the existence of Value2.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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