VBA Excel:防止 Excel 将字符串格式化为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17574487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Excel: Prevent Excel to format string as date
提问by karamell
Quick question:
快速提问:
I want to insert a string of type 10/3
in a cell like this:
我想10/3
在像这样的单元格中插入一个类型的字符串:
Worksheets("Sheet1").Range("A1").Value = "10/3"
But Excel automatically formats the cell as a date or a number. I want to have it as a string.
但 Excel 会自动将单元格格式化为日期或数字。我想把它作为一个字符串。
How can I achieve this with VBA?
如何使用 VBA 实现这一目标?
回答by funk
Worksheets("Sheet1").Range("A1").NumberFormat = "@"
Worksheets("Sheet1").Range("A1").Value = "10/3"
回答by SeanC
add a single quote '
before the value - in your example, it would make the line
'
在值之前添加一个单引号- 在您的示例中,它将使该行
Worksheets("Sheet1").Range("A1").Value = "'10/3"
or, if you have a variable that holds the data
或者,如果您有一个保存数据的变量
Worksheets("Sheet1").Range("A1").Value = "'" & MyValue