excel - 在 Debug.Print 中返回 5 位日期而不格式化单元格

标签 excel vba

背景:

我希望从格式化为显示日期的单元格中获取 5 位日期。 5 位日期只能是立即窗口(通过 debug.print)。

我的测试结果位于脚本部分(底部)。

根据我所做的测试,我有一种感觉,答案将涉及 datediff()+2 ,但我不明白为什么需要 +2 并且不想这样做以后如果有错的话补充一下。

<小时/>

问题:

我似乎无法通过 Debug.Print 显示正确的 5 位日期(以“常规”格式显示)。

<小时/>

问题:

如何在不首先转换单元格的 .numberformat< 的情况下,将 5 位日期(通常以常规范式显示)显示在立即窗口 (debug.print) 中

<小时/>

脚本:

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

最佳答案

Range.Value2不使用 Date 类型,因此您只需 Debug.Print rng.Value2

关于excel - 在 Debug.Print 中返回 5 位日期而不格式化单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58610973/

相关文章:

excel - 间接求和

mysql - 报告所有可能的列组合

数据的 VBA 排序

excel - 如何用vba创建多个图表

excel - 仅将可见工作表中的可见单元格复制到新工作簿中,excel 2007 VBA

excel - 当输入范围是字符串时,为什么我的组合框宏不起作用?

javascript - VBA : access an HTML elements without Id or Name

Python,解析列数据,pandas

excel - VBA 编辑器自动删除行尾的空格

excel - 如何对范围内的一个单元格值进行编号,然后在单元格值发生变化时重新开始编号