日期为何返回“ 31-12-1899”?当1传递给它时? [英] Why does the date returns "31-12-1899" when 1 is passed to it?

查看:165
本文介绍了日期为何返回“ 31-12-1899”?当1传递给它时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Windows 10 OS excel 13,因此在下面的代码 1 应该返回 1/1/1900 对吗?





请注意,您可以获取 1/1/1900

之前的日期

EDIT#1:



这可能有助于理解差异。我在 A 列中放入了一些整数。



B1 中,我放入了 = A1 并复制下来。我格式化 B 列以日期格式显示。



我使用此UDF():

 公共函数VBA_Date(i as Long)如字符串
VBA_Date = Format(CDate(i), mm / dd / yyyy)
结束函数

填充列 C





请注意第19行和第20行之间的过渡


I am using windows 10 OS excel 13 so in the below code 1 should return 1/1/1900 right ? below it doesn't why.

On this question OP passed 2016 and got the same result I got so if there is some error why do I got the same result as OP when I passed 2016 ?

Sub ndat()

Dim dt As Date
dt = 2016
Debug.Print "dt is " & dt

End Sub

解决方案

Integers and dates map differently in VBA than in the worksheet. For example:

Sub marine()
    Dim i As Integer, d As Date
    Dim mgs As String

    msg = ""
    For i = -10 To 10
        d = CDate(i)
        msg = msg & i & vbTab & Format(d, "mm/dd/yyyy") & vbCrLf
    Next i

    MsgBox msg
End Sub

Produces:

Note you can get dates prior to 1/1/1900

EDIT#1:

This may help to understand the difference. I put some integers in column A.

In B1, I put =A1 and copy down. I format column B to display in date format.

I use this UDF():

Public Function VBA_Date(i As Long) As String
    VBA_Date = Format(CDate(i), "mm/dd/yyyy")
End Function

To fill column C:

Note the transition between rows #19 and #20

这篇关于日期为何返回“ 31-12-1899”?当1传递给它时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆