Why does cell format change when assigning a value?

N

N Selinger

I have some VBA code which accumulates a total of several cells, then puts
that total into a cell formatted as "Accounting" format. Every time this
code runs, it changes the cell formatting to "Currency".

I have tested this with a new, blank workbook and I get the same result.
Try the following:

Sub test()
a = 10
Cells(2, 1) = 0
a = a + Cells(2, 1)
Cells(3, 1).Value = a
End Sub

Why does it change the formatting??? And how do get around this problem?
 
J

JoeU2004

N Selinger said:
I have some VBA code which accumulates a total of several cells, then
puts that total into a cell formatted as "Accounting" format. Every time
this code runs, it changes the cell formatting to "Currency".
[....]
And how do get around this problem?

Try the following test code:

Range("A2").Value2 = Range("A2") + 1

Note the use of Value2 instead of Value.

Why does it change the formatting???

In Unix, we have a saying: "don't ask why" ;-). But the following might
provide a hint:
http://msdn.microsoft.com/en-us/library/aa215938(office.11).aspx .


----- original message -----
 
J

JoeU2004

Let's try this again, this time with proper explanation....

N Selinger said:
I have some VBA code which accumulates a total of several cells, then
puts that total into a cell formatted as "Accounting" format. Every time
this code runs, it changes the cell formatting to "Currency".
[....]
And how do get around this problem?

Put a value into A2 and format A2 as Accounting.

Then try the following test code:

Range("A2").Value2 = Range("A2") + 1

Note the use of Value2 instead of Value.

Why does it change the formatting???

In Unix, we have a saying: "don't ask why" ;-). But the following might
provide a hint:
http://msdn.microsoft.com/en-us/library/aa215938(office.11).aspx .


----- original message -----
 

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