且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用VBA将单元格的格式更改为文本

更新时间:2023-02-13 19:55:30

为了回答您的直接问题,它是: / p>

 范围(A1)。NumberFormat =@

  Cells(1,1).NumberFormat =@ 

但是,我建议将格式更改为您想要显示的内容。这允许您在单元格中保留数据类型,并可轻松使用单元格公式来操作数据。


I have a "duration" column in an Excel sheet. Its cell format always changes — I want convert the duration from minutes to seconds, but because of the cell formatting it always gives me different answers.

I was thinking that before doing the conversion I could convert that cell format to text so that it will consider that as text value and not try to auto-format it.

Currently I am copying all data into Notepad and then saving it back to the Excel sheet to remove all of the previous format. Is there a way to automate setting a cell's formatting to text using VBA?

To answer your direct question, it is:

Range("A1").NumberFormat = "@"

Or

Cells(1,1).NumberFormat = "@"

However, I suggest making changing the format to what you actually want displayed. This allows you to retain the data type in the cell and easily use cell formulas to manipulate the data.