我试图确定包含其他文本的字符串中的日期是否小于另一个单元格中的日期,但是由于某种原因,即使日期格式与我的Like匹配,该函数也似乎从未命中日期公式声明条件。
这是我尝试从其中提取的一个单元格的示例 1/9/2018 5:02AM Benjamin Button
请看下面的代码
Sub findAndCheck
for i = 2 to lastrow ''Lastrow declared and set, just not shown here [i is global]
if Getdate(cells(i,68).value2) then
comments = "True"
end if
next i
End Sub
Public Function Getdate(expression As Variant) As Boolean
Dim dateconvert As Date
If (expression Like "*#/#/####*") Then
dateconvert = CDate(Left(expression, 8))
Getdate = CDate(expression) < CDate(Cells(i, 66))
ElseIf (expression Like "*##/#/####*") Or (expression Like "*#/##/####*") Then
dateconvert = CDate(Left(expression, 9))
Getdate = CDate(expression) < CDate(Cells(i, 66))
ElseIf (expression Like "*##/##/####*") Then
dateconvert = CDate(Left(expression, 10))
Getdate = CDate(expression) < CDate(Cells(i, 66))
End If
End Function
您没有i
将函数作为参数包含在内,所以我做了。
另外,只需检查您是否expression
包含空格,然后对该空格进行拆分即可。
Public Function Getdate(expression As Variant, i As Long) As Boolean
Dim rawDate As Date
If InStr(1, expression, " ") > 0 Then
rawDate = CDate(Split(expression, " ")(0))
End If
Getdate = rawDate < CDate(Cells(i, 66))
End Function
您还应该检查rawDate = 0
,这意味着您的字符串根本不包含日期。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句