【问题标题】:Find and extract date from string not working?从字符串中查找并提取日期不起作用?
【发布时间】: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 变量中创建的日期:)

标签: vba excel


【解决方案1】:

您没有将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,这意味着您的字符串根本不包含日期。

【讨论】:

  • 上面的子程序中处理了“i”,在其他程序中似乎可以正常工作?不过,我在这一行的最后一行出现错误?它吐出类型不匹配(错误 13)
  • @Gurrito 如果您在模块顶部使用Option Explicit,您会意识到它并不“好”。 i 的值是 0(或者更具体地说是 Null),因为您没有将它传递给您的函数。可以由您的 sub 处理,但 i 不是全局变量,因此虽然它可能由您的 sub 处理,但与您的函数无关
  • @Gurrito 在该行上方您会收到错误类型:Debug.Print rawDate(新行)然后是Debug.Print Cells(i, 66)。您将能够在您的即时窗口中查看此内容。他们都是日期吗?
  • @k.davis,我是我项目中的一个全局变量。
  • 我犯了一个愚蠢的错误(调用错误工作表中的单元格)。这个解决方案是正确的,尽管我确实必须将空的 " "s 更改为 chr(32),如 Jeeped 的解决方案中所示,以获取空间。谢谢!
【解决方案2】:

isdate(split("1/9/2018 5:02AM Benjamin Button", chr(32))(0)) 应该确定您是否有提前日期。

Public Function Getdate(expression As Variant, _
                        optional i as long = 2) As Boolean
    If isdate(split(expression, chr(32))(0)) Then
        Getdate = CDate(split(expression, chr(32))(0)) < CDate(Cells(i, 66))
    End If  
End Function

【讨论】:

    【解决方案3】:

    你的问题在这里:

    Getdate = CDate(expression) < CDate(Cells(i, 66))
    

    您将无法评估CDate(expression),因为表达式的全文仍包含除日期戳之外的其他内容。我想您想检查您的日期转换是否小于该单元格中的值:

    Getdate = dateconvert < CDate(Cells(i, 66))
    

    您还需要在某处声明i。大概您希望它作为函数中的另一个参数(例如):

    Public Function Getdate(expression As Variant, i as integer) As Boolean
    

    【讨论】:

    • 感谢这个新痰。现在的问题是它甚至没有下降到这个水平。 if/elseifs 返回不正确
    【解决方案4】:

    要从与您显示的类似的表达式中提取日期(日期位于字符串的开头、结尾或由空格分隔的字符串的其余部分,您可以使用:

    Option Explicit
    Function GetDate(sExpression As String) As Date
        Dim v, w
    v = Split(sExpression, " ")
    For Each w In v
        If IsDate(w) Then
            GetDate = CDate(w)
            Exit Function
        End If
    Next w
    
    End Function
    

    如果没有日期,该函数将返回零

    并且,要比较两个单元格,例如:

    Sub findAndCheck
     for i = 2 to lastrow ''Lastrow declared and set, just not shown here [i is global]
      if Getdate(cells(i,68) > getdate(cells(i,66) and _
         getdate(cells(i,68) <> 0 and getdate(cells(i,66) <> 0 _
     then
        comments = "True"
      end if
     next i
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 2021-01-13
      • 2020-08-14
      • 2016-06-24
      相关资源
      最近更新 更多