Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.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,我有各种格式的日期,在字符串中,都在ColumnA中。这里有一个小例子 -rw-r--r-- 35 30067 10224 <-- 2018-09 -rw-r--r-- 36 30067 10224 <-- 2018-09 -rw-r--r-- 65 30067 10224 <-- 2018-10-24 -rw-r--r--353006710224如果没有任何固定的模式,它将是复杂的。 在您的案例中,逻辑上更好的方法是将日

我有各种格式的日期,在字符串中,都在ColumnA中。这里有一个小例子

-rw-r--r--   35 30067    10224     <-- 2018-09
-rw-r--r--   36 30067    10224     <-- 2018-09
-rw-r--r--   65 30067    10224     <-- 2018-10-24

-rw-r--r--353006710224如果没有任何固定的模式,它将是复杂的。
在您的案例中,逻辑上更好的方法是将日期模式与以下文件关联

  • 1=>*.总线->YYMM
  • 2=>*.IDE->YYMMDD
  • 3=>TTN*->YYYYMMDD
在相同的逻辑上,得到一个映射表,其中日期字符串在文件名中的位置

  • 1=>文件名的最后4个字符
  • 2=>文件名的最后6个字符
  • 3=>扩展
在宏函数或开关函数中

试试看

Sub test()
    Dim rngDB As Range
    Dim vDB As Variant, vR() As Variant
    Dim vS As Variant
    Dim i As Long, j As Integer, c As Integer
    Dim s As String

    Set rngDB = Range("a1", Range("a" & Rows.Count).End(xlUp))
    vDB = rngDB

    r = UBound(vDB, 1)
    ReDim vR(1 To r, 1 To 1)

    For i = 1 To r
        s = vDB(i, 1)
        vS = Split(s, ".")
        If IsNumeric(vS(1)) Then
            s = vS(1)
            s = Right(s, 6)
            s = Left(s, 2) & " " & Mid(s, 3, 2) & " " & Right(s, 2)
            vR(i, 1) = DateValue(s)
        Else
            s = vS(0)
            s = Right(s, 6)
            If IsNumeric(s) Then
                s = Left(s, 2) & " " & Mid(s, 3, 2) & " " & Right(s, 2)
                vR(i, 1) = DateValue(s)
            Else
                s = Right(s, 4)
                If IsNumeric(s) Then
                    s = Left(s, 2) & " " & Mid(s, 3, 2) & " " & 1
                    vR(i, 1) = DateValue(s)
                Else
                    s = vS(0)
                    s = Right(s, 7)
                    s = Left(s, 2) & " " & Mid(s, 3, 3) & " " & Right(s, 2)
                    vR(i, 1) = DateValue(s)
                End If
            End If
        End If
    Next i
    Range("b1").Resize(r) = vR

End Sub

我试图用正则表达式得到日期。据我所见,日期要么在字符串的末尾,要么在后面有一个点(后跟扩展名)。正则表达式考虑了一年的长度:4或2。它还管理以文本表示月份的情况。重要的当这一天不在的时候,我把它定为第一天

Function GetDate(strString$)
    Dim sYear$, sMonth$, sDay$
    With CreateObject("VBScript.RegExp")
        .IgnoreCase = True: .Pattern = "(\d{2})([a-z]{3}|\d{2})(\d{2})?(?=\.|$)"
        With .Execute(strString)
            If .Count > 0 Then
                With .Item(0)
                    sYear = .SubMatches(0)
                    sMonth = .SubMatches(1)
                    sDay = .SubMatches(2)
                    sYear = "20" & sYear
                    If Not IsNumeric(sMonth) Then
                        sMonth = GetMonthIndex(sMonth)
                    End If
                    If Len(sDay) = 0 Then sDay = "01"
                    GetDate = DateSerial(CInt(sYear), CInt(sMonth), CInt(sDay))
                End With
            End If
        End With
    End With
End Function

Private Function GetMonthIndex$(strMonth$)
    Select Case strMonth
        Case "Jan": GetMonthIndex = "01"
        Case "Feb": GetMonthIndex = "02"
        Case "Mar": GetMonthIndex = "03"
        Case "Apr": GetMonthIndex = "04"
        Case "May": GetMonthIndex = "05"
        Case "Jun": GetMonthIndex = "06"
        Case "Jul": GetMonthIndex = "07"
        Case "Aug": GetMonthIndex = "08"
        Case "Sep": GetMonthIndex = "09"
        Case "Nov": GetMonthIndex = "10"
        Case "Oct": GetMonthIndex = "11"
        Case "Dec": GetMonthIndex = "12"
    End Select
End Function

看起来您可以通过使用开关进行扩展来处理此问题,例如,案例1-->右(路径,4)=.zip-->左(路径,11),1)=yr等。需要做一些设置工作,但您的命名约定似乎是一致的。这至少是一个选项。某些格式可以处理,但也可能出现误报。。。例如
GMD\U ASE1JPN\U ID\U 181031--2018-10-31
GMD\U ASE1JPN\U ID\U 181019的情况如何
。这是2019年10月18日还是2018年10月19日?始终是年-月-日。为什么要返回前两行
2018-09
?然后在2018-09-28日晚些时候?我把第二个和第三个,
20181024
转换成了
2018-10-24
,但是你在哪里为那些没有这种格式的人预测了日期呢?请你解释一下日期翻译,翻译的日期似乎在实际日期之前的一个月。你说的不同格式是什么意思?在我看来,所有日期都是相同格式的
mmmdd
我测试了你的代码。某些行出现错误。(上例中的第6-13行)@Dy.Lee OK。我来看看。我还没有测试你的代码。但是你的代码很漂亮。非常好用!!非常感谢!!用于
GetMonthIndex函数的简单一行代码
GetMonthIndex=Format(CDate(strMonth&“/10”),“mm”)
:-)