Excel应用程序与1904日期系统匹配

时间:2015-11-16 15:21:50

标签: excel excel-vba vba

我目前有一段代码可以在电子表格中搜索特定日期,使用1900日期系统可以正常工作,但在尝试使用1904日期系统时会出现类型不匹配13错误。两个工作簿都设置为1904年日期系统。

Dim fDate As Date
Dim rw As Long
Dim name As String
Dim srchRange As Range
Dim book2name As String, book2 As Workbook

book2name = "Master_Sheet.xlsm"   ' name of Source File
Set book2 = Workbooks(book2name)
name = "SM"

fDate = GetWeekStartDate(Range("C12"))
Set srchRange = book2.Sheets(name).Range("B:B")    ' Search Range

If Application.CountIf(srchRange, fDate) Then
    rw = Application.Match(CLng(fDate), srchRange, 0)
Else
    MsgBox Format(fDate, "dd/mm/yyyy") & " not found."
End If

Function GetWeekStartDate(ByVal strdate, Optional ByVal lngStartDay As Long = 2) As String
GetWeekStartDate = DateAdd("d", _
                   -Weekday(CDate(strdate), lngStartDay) + 1, CDate(strdate))
End Function

1 个答案:

答案 0 :(得分:0)

由于1904年日期系统,单元格的基础值与1900日期系统不同,即使日期显示相同。这意味着您需要将Match的值偏移1462才能找到它:

rw = Application.Match(CLng(fDate) - 1462, srchRange, 0)
相关问题