背景:
我想从格式化显示日期的单元格中获取5位数字的日期。5位数字的日期只能是即时窗口(通过debug.print
)。
我的测试结果在“脚本”部分(底部)中。
我感觉答案将datediff()+2
基于我所做的测试,但是我无法弄清楚为什么需要+2,并且如果将来出错,也不想仅仅添加它。
问题:
我似乎无法通过来显示正确的5位日期(以“常规”格式显示)Debug.Print
。
题:
如何获得5位日期(通常以通用格式显示)显示在立即窗口(debug.print
)中而不先转换单元格的日期.numberformat
?
脚本:
Dim rng As Range, d1 As String, d2 As String
Set rng = Cells(1, 1) 'value = 20190101
Debug.Print Format(rng.Value, "@") 'returns 20190101
Debug.Print rng.NumberFormat = "General" 'returns "false"
Debug.Print Format(rng.value, "General") 'returns "Ge0oral" due to the "n" being recognized for a format
d1 = "1900/01/01"
d2 = rng.Value
Debug.Print 40729 + DateDiff("d", d1, d2) 'returns 84195
Debug.Print DateDiff("d", d1, d2) 'returns 43466
rng.NumberFormat = "General"
Debug.Print rng.Value 'returns 43468, but required formatting the cell
当您要打印日期的数字表示而不更改单元格的格式时,会想到两种方法。
最好的方法是使用该.Value2
属性。
Debug.Print Rng.Value2
“不建议使用”的方法是使用该CDbl()
功能。我还会将此用于变量和.Value2
范围。
Debug.Print CDbl(Rng.Value)
或用于变量
Dim d as Date
d = #01/01/2019#
Debug.Print CDbl(d)
Debug.Print CLng(d) 'If not using time - date only
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句