VBA Excel:我是否发现了Excel在1900年如何处理“闰年”的错误? (这不是闰年)

免责声明:这是一个相当具体的错误,我想我在Excel中遇到了,但它是奇怪的,我永远不会find一个解决它的答案,如果甚至可能,如果我不在这里发布。 这是一个有趣的,但我很担心。

因此,在一个上下文中,我写了一些代码,当我input列B时,可以让我捕获列A中的时间戳。 我添加了一点,所以它提示用户,如果他们确定他们想要更改input后,以保持时间戳的完整性。 这也适用。

但在testing过程中遇到了一个问题。 如果我只是在列A中input一个数字,例如数字5,它会强制它的格式为1/5/1900 00:00。 对我来说很有意义,因为Excel明确地select1900作为任意的起点。 但是,如果我尝试编辑这个date,提示问我是否要改变它显示为1/4/1900(截图如下)。 最长的时间我不明白为什么它是closures的。我意识到,1至60(date1日至2月29日)之间的任何input引起这个问题。 3/1/1900之后的任何date都提示我正确。 我做了一些挖掘工作,发现1900年没有闰年,所以60年代应该解决到3/1/1900,而在Excel中则是2/29/1900。

我知道这是一个小问题,只影响A列中的60个条目,但我觉得无能为力,因为看起来问题在于Excel如何计算1900年的闰年,而不应该这样做。 有没有人有一个解决方法,或至less确认这是Excel的问题,而不是我的代码?

3例编辑栏A截图

预计第一和第三的结果。 中间一个显示,即使前一个字段实际上是1月5日,提示显示为1月4日。我会张贴我有下面的代码,以防万一任何人想要尝试相同的事情。

Private lastVal As Variant Private Sub Worksheet_Change(ByVal Target As Range) Dim timeCol, taskCol As Integer Dim sRow, sCol As Integer Dim currentCell As Range timeCol = 1 taskCol = 2 sRow = Target.row sCol = Target.Column Set currentCell = Cells(sRow, sCol) 'Checks for timestamp or task column. If Cell wasn't empty, prompt user to 'confirm the change If sCol = timeCol Or sCol = taskCol Then If lastVal <> "" Then response = MsgBox("Old Value: " & lastVal & Chr(10) & Chr(10) _ & "New Value: " & Cells(sRow, sCol) & Chr(10) & Chr(10) _ & "Are you sure you want to make this change?", _ vbYesNo + vbQuestion, "Change Confirmation") If response = vbYes Then 'Do nothing Else 'Reject the change Application.EnableEvents = False currentCell = lastVal Application.EnableEvents = True End If End If End If 'Checks for task column. If timestamp cell at current row is empty 'and something was entered into task column, fills timestamp cell If sCol = taskCol Then If Cells(sRow, timeCol) = "" = True Then If currentCell <> "" Then Application.EnableEvents = False Cells(sRow, timeCol) = Now() Application.EnableEvents = True End If End If End If End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim sRow, sCol Dim currentCell As Range sRow = Target.row sCol = Target.Column Set currentCell = Cells(sRow, sCol) lastVal = currentCell End Sub 

这里是:

当Lotus 1-2-3首次发布时,该计划假定1900年是一个闰年,即使它实际上不是一个闰年。 这使得程序更容易处理闰年,并且几乎不会影响Lotus 1-2-3中的所有date计算。

当Microsoft Multiplan和Microsoft Excel发布时,他们还认为1900年是一个闰年。 这个假设允许Microsoft Multiplan和Microsoft Excel使用Lotus 1-2-3使用的相同的序列date系统,并提供与Lotus 1-2-3更好的兼容性。 1900年作为一个闰年,也使用户更容易将工作表从一个程序移动到另一个程序。

尽pipe在技术上可以纠正这种行为,以便当前版本的Microsoft Excel不会假设1900年是闰年,但这样做的缺点超过了优点。

https://support.microsoft.com/en-us/help/214326/excel-incorrectly-assumes-that-the-year-1900-is-a-leap-year

这是由原始Excel团队 有意引入的一个众所周知的错误 ,以保持与Lotus 1-2-3的 兼容性 。