It seems that VBA was changed in Excel 2016/2013 as it starts throwing errors in places where it does not drop Excel 2010.
The code below does the following:
- Creates 17 dates in the first row of ActiveSheet
- populates a date array, with 4 values
- one in the first line
- one is not in the first row
- alone in 1913
- one until 1904
- he searches for each value and colors the cell red if it is found;
In Excel 2010, it runs smoothly, finding one value and not searching for the other 3 as expected. Everything is good.
In Excel 2016/2013, it becomes a displeased value until 1904 and throws an error
Invalid procedure call or argument (Error 5)
on Set foundRange = Rows(1).Find(someDates(cnt)) .
So it seems that in Excel 2016/2013, according to Date1904, there is some check that does that 04.01.1900 until the year 1904 and therefore it cannot be parsed with a date in the Excel date system? Although in Excel 2010 this is not so.
So the question is - Is this function / behavior documented?
Public Sub TestMe() ThisWorkbook.Date1904 = True Cells.Clear 'clearing up all. Dim cnt As Long For cnt = 3 To 20 Cells(1, cnt) = DateAdd("M", cnt, DateSerial(2016, 1, 1)) Cells(1, cnt).NumberFormat = "MMM-YY" Next cnt Dim someDates(3) As Date someDates(0) = DateSerial(2016, 1, 1) 'exists someDates(1) = DateSerial(2012, 1, 1) 'does not exist in the range someDates(2) = 5000 '08.09.1913 (in VBA) someDates(3) = 5 '04.01.1900 (in VBA) Dim foundRange As Range For cnt = LBound(someDates) To UBound(someDates) Set foundRange = Rows(1).Find(someDates(cnt)) 'Error 5 in Excel 2016 If Not foundRange Is Nothing Then foundRange.Interior.Color = vbRed End If Next cnt ThisWorkbook.Date1904 = False 'all dates with 4 years back End Sub
Why November 2016 was selected when searching in January 2016 - Range. Do not assume that the difference between January and November (February and December) in VBA Excel
date vba excel-vba excel version
Vityata
source share