Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何在字符串中搜索特定的数字?_Excel_Vba - Fatal编程技术网

Excel 如何在字符串中搜索特定的数字?

Excel 如何在字符串中搜索特定的数字?,excel,vba,Excel,Vba,我有一个代码,提示用户在他们的计算机上查找特定的文件。这些文件的名称如下“每日操作报告-日-月-年” 我需要确保选择的文件与今天的日期相同。我知道如何比较两个日期,看它们是否相等 如何在字符串中查找数字 'Previous code not relevant to the question Dim strFilePath, fName As String Dim wdDoc As Object strFilePath = Application.GetOpenFilename If

我有一个代码,提示用户在他们的计算机上查找特定的文件。这些文件的名称如下“每日操作报告-日-月-年”

我需要确保选择的文件与今天的日期相同。我知道如何比较两个日期,看它们是否相等

如何在字符串中查找数字

'Previous code not relevant to the question

Dim strFilePath, fName As String
Dim wdDoc As Object

strFilePath = Application.GetOpenFilename
    If strFilePath = "False" Then End  'Pressed cancel

fName = strFilePath

Set wdDoc = GetObject(fName)
执行上述代码后,我可以在(wdDoc)中找到文件名,例如:

 MsgBox wdDoc
我得到:

每日运营报告——2019年12月21日


如何将文件与今天的日期进行比较?我相信比较天数比比较两个日期的天数、月份和年份更容易、更快。

有很多方法可以做到这一点,但这里有一种。它接受文件名并将其转换为日期。你用那个日期做什么比较取决于你自己

Function GetDateFromFilename(sFilename As String) As Date
  Dim sFullDate As String
  Dim sDay As String
  Dim sNum As String
  Dim nDig As Integer: nDig = 1

  ' get rid of the extension just in case it exists
  sFilename = Split(sFilename, ".")(0)

  ' get just the date part for processing
  If Instr(sFilename, " - ") = 0 Then
    Msgbox "Filename not in proper format"
    GetDateFromFilename = CDate("1/1/1990")
    Exit Function
  End If
  sFullDate = Split(sFilename, " - ")(1)

  ' get the first digit of that
  sNum = Left$(sFullDate, nDig)

  ' loop thru the characters until there is an alpha character
  Do While nDig <= Len(sFullDate)
    If IsNumeric(sNum) Then
      sDay = sDay & sNum
    Else
      ' found the first alpha, so we are done here
      Exit Do
    End If
    ' check the next character
    nDig = nDig + 1
    sNum = Mid$(sFullDate, nDig, 1)
  Loop

  ' build a string that correctly resembles a date (month first)
  sFullDate = Split(sFullDate)(1) & " " & sDay & " " & Split(sFullDate)(2)

  ' or build a string that correctly resembles a date (day first)
  ' sFullDate = sDay & " " & Split(sFullDate)(1) & " " & Split(sFullDate)(2)

  ' convert it to an actual date and return it
  GetDateFromFilename = CDate(sFullDate)

End Function
注意:如果您的文件名字符串不是此格式,则很有可能出错,但您的问题暗示它将始终是此格式

Dim dtFileDate As Date
dtFileDate = GetDateFromFilename("Daily Operations Report - 21st December 2019")
' show just the day number
MsgBox Format$(dtFileDate, "d")