如何使用 VBA 将日期(mm/dd/yyyy)与时间(hh:mm:ss)连接起来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19781234/
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
How to concatenate date(mm/dd/yyyy) with time (hh:mm:ss) using VBA
提问by Neli S Gautham Raj
I have two cells: A1 and B1
我有两个单元格:A1 和 B1
A1 has data in format of
mm/dd/yyyy
B1 has data in format of
hh:mm:ss
A1 的数据格式为
mm/dd/yyyy
B1 的数据格式为
hh:mm:ss
When I concatenate the two cells A1 and B1 using VBA, the output contains has the format mm/dd/yyyy hh:mm
. Why are the seconds not displaying? How can I fix this?
当我使用 VBA 连接两个单元格 A1 和 B1 时,输出包含的格式为mm/dd/yyyy hh:mm
. 为什么不显示秒?我怎样才能解决这个问题?
Note :
笔记 :
a) After concatenation the output is copied to A1.
a) 连接后,输出被复制到 A1。
b) I tried changing the format of date to dd/mm/yyyy
, in this case its working fine .
b)我尝试将日期格式更改为dd/mm/yyyy
,在这种情况下它工作正常。
回答by sam092
Try to change the format of C1 to
mm/dd/yyyy hh:mm:ss
尝试将 C1 的格式更改为
mm/dd/yyyy hh:mm:ss
回答by Jean-Fran?ois Corbett
It's hard to tell what you are doing exactly without seeing your code, but I think you are specifying the format of the value you are writing to cell A1: using the VBA Format
function you are converting the date-time into a string.
如果不看代码,很难确切地知道您在做什么,但我认为您正在指定写入单元格 A1 的值的格式:使用 VBAFormat
函数将日期时间转换为字符串。
However, to change the way the date is displayedin a cell, you need to change the format of that cell itself(not the format of the valuewritten to it). Otherwise Excel will likely interpret the value in the cell and snap it back to the specified number format for that cell.
但是,要更改日期在单元格中的显示方式,您需要更改该单元格本身的格式(而不是写入其中的值的格式)。否则,Excel 可能会解释单元格中的值,并将其恢复为该单元格的指定数字格式。
You can change the cell's format in the Excel 2010 window like this: Home > Number > Custom > Type: mm/dd/yy hh:mm:ss
您可以在 Excel 2010 窗口中更改单元格的格式,如下所示:主页 > 数字 > 自定义 > 类型: mm/dd/yy hh:mm:ss
Or, using VBA:
或者,使用 VBA:
Range("A1").NumberFormat = "mm/dd/yy hh:mm:ss"