You are here:

Excel/Retaining Number formats in VBA


I have some code that transfers values from one worksheet and arranges them in another worksheet. The number formats are the same for each column on both worksheets, however, somehow the number formats change when transferring the data. For example, I have some columns formatted to the Accounting Format, but some of the values transferred to that column show up in the Currency Format. I am using the .value property to transfer the data... should I use the .text property or should is there a way to 'Lock' the formats in the receiving worksheet so that the formats cannot change when new data is added?

Thanks for the help,


if you do a

pastespecial xlvalues

then formats will not be altered in the receiving sheet.  

If Excel is trying to be too helpful on it own, I can't account for that.

Tom Ogilvy

---------- FOLLOW-UP ----------

QUESTION: Here is an example of the code I'm using... I'm not sure that pastespecial would be appropriate:

sh4.Cells((rw4 + i) - 2, "CU").Value = sh4.Cells(rw4 + i, "AH").Value

Is there anything in that statement that would cause the number format to change? Perhaps I should format each value within the code itself...

Format(sh4.Cells((rw4 + i) - 2, "CU").Value, "Percent")

Any suggestions?


No, that would not change the format.

possibly you could add code to copy and paste the format.

sh4.Cells((rw4 + i) - 2, "CU").Value = sh4.Cells(rw4 + i, "AH").Value
sh4.Cells(rw4 + i, "AH").Copy
sh4.Cells((rw4 + i) - 2, "CU").PasteSpecial xlFormats

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 All rights reserved.