【发布时间】:2018-02-02 05:20:36
【问题描述】:
我正在尝试确定包含其他文本的字符串中的日期是否小于另一个单元格中的日期,但由于某种原因,该函数似乎永远不会命中日期公式,即使日期格式匹配我的 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
【问题讨论】:
-
这个位在我看来是个错误:Cells(i, 66)。它应该做什么?
-
你能告诉我们你得到了什么输出吗?
-
@GregViers 我的错误。那应该是cells(i,68).value2。这是一个循环中的值,我只是删掉了那段代码以使其更易于阅读。我现在把它加回来了。
-
@neophlegm 实际上没有。它检查函数中的所有 3 个条件,并且不满足其中任何一个。
-
@Gurrito 好吧,我就是这么想的。请参阅我的回答:它没有评估
expression中的日期,因为您试图从整个文本中提取日期,而不是使用您已经在dateconvert变量中创建的日期:)