Number stored as text. How do I keep the number format?

S

staying

Hi, all.

I came across a problem when trying to put a value from one cell t
another.
Here goes a sample code.

=======================
Sub aaa()
Dim rng_src as range

Set rng_src = activecell
activecell.offset(1,0).value = rng_src.value
activecell.offset(1,0).NumberFormat = rng_src.NumberFormat

End Sub
=======================

It works fine with regular numeric or string values, but gets ver
frustrating when the source cell is "number stored as text", whethe
the cell format was overriden with an apostrophe or it was value-copie
from such ranges.

I can't find anything in the vba help file or the object browser.

Is there any way to work around this pain in the 's':mad: , other tha
PasteSpecial method?

Thanks a lot
 
G

Guest

I forgot the a single quote ( apostrophe) is a special case:

Sub bbb()
Dim rng_src As Range

Set rng_src = ActiveCell
ActiveCell.Offset(1, 0).NumberFormat = rng_src.NumberFormat
If rng_src.PrefixCharacter = "'" Then
ActiveCell.Offset(1, 0).Value = Chr(39) & rng_src.Value
Else
ActiveCell.Offset(1, 0).Value = rng_src.Value
End If
End Sub

seems to work in all cases
 
S

staying

Thanks, Gary''s Student.

Your solution works well except for one case. The problem persists whe
you copy a cell which was text formatted with an apostrophe and valu
paste it into another cell.

Is there a hidden property in those cells
 

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