Which date system does VBA recognise/default to?

后端 未结 2 1821
别跟我提以往
别跟我提以往 2021-01-06 10:38

I\'m writing a vba subroutine in excel to store all date values between 2 dates in a single dimension array and then check for each value in a range on another sheet.

相关标签:
2条回答
  • 2021-01-06 10:45

    To provide a solution to my example a colleague mentioned if you dim the date values as the date datatype the issues with the values going sheet -> vba -> sheet disappears.

    0 讨论(0)
  • 2021-01-06 10:53

    In general, the date system in VBA is different than the date system in Excel. You can have these 3 date system options:

    • Excel date
    • Excel date with 1904 property
    • VBA date

    This is the difference:

    The dates are converted to numbers. E.g., every date is converted to a number, but the starting number is a bit different.

    • In Excel, the 1 is converted to 01.January.1900;
    • In VBA, the 1 is converted to 31.December.1899;
    • In Excel with 1904, the 1 is converted to 02.January.1904;

    The 1904 system (or its lack) is set per Workbook. Thus, if you have 2 workbooks in Excel, one with 1904 and one without it, it would recognize them correspondingly. The system is set in:

    File > Options > Advanced > Use 1904 Date System

    In general, if there is a chance, that someone somehow changes your workbook with the wrong system, consider adding this check at the openning of the workbook:

    Private Sub Workbook_Open()    
        If ThisWorkbook.Date1904 Then
            MsgBox "System is 1904, consider some action!"
        End If        
    End Sub
    

    If you are wondering which system to choose - I can recommend never using the 1904.

    • Story for the adding VBA to Excel and the 1904 problem
    0 讨论(0)
提交回复
热议问题