简体   繁体   English

excel vba 更改日期格式 - 数字

[英]excel vba change format of date - numbers

Excel changes the following values automatically to a number, I guess because he considers them as a date: Excel 自动将以下值更改为数字,我猜是因为他将它们视为日期:

2/2/1 becomes 36527 4/2/1 becomes 36926 2/2/1 变成 36527 4/2/1 变成 36926

I have a column with a combination of different formats now:我现在有一个包含不同格式组合的列:

2/1/
3/1/
8/7/
36527
1/0/0
36926

Which VBA code can I use to convert the numbers back to their original format?我可以使用哪个 VBA 代码将数字转换回原始格式? The other values should stay the same.其他值应保持不变。

I know the cDate function, but I guess it's not useful here?我知道 cDate 函数,但我想它在这里没有用?

I have already this in my VBA code for pasting values我已经在我的 VBA 代码中使用了这个来粘贴值

ActiveWorkbook.Sheets("Import").Columns("A:AH").NumberFormat = "@"
ActiveWorkbook.Sheets("Import").Range("A1").Select
ActiveSheet.Paste

You can't change them back.你不能把它们改回来。 Once Excel converts them, the original value is gone.一旦 Excel 转换它们,原始值就消失了。 Before you input the value, you can prepend an apostrophe to force it to text.在输入值之前,您可以在前面加上撇号以将其强制为文本。

ActiveCell.Value = "'" & sMyValue

or as @Scott Craner commented, you can format the cell as text或者正如@Scott Craner 评论的那样,您可以将单元格格式化为文本

ActiveCell.NumberFormat = "@"
ActiveCell.Value = sMyValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM