我在ColumnA中都有各种日期格式的字符串形式的日期。这是一个小例子。
========================
Call by Name false
Time 64ms
========================
Call by Value false
Time 3068ms
========================
答案 0 :(得分:2)
我试图用正则表达式获取日期。据我所知,日期要么在字符串的末尾,要么在字符串的后面有一个点(后跟扩展名)。正则表达式考虑了年份的长度: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 :(得分:1)
尝试
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
答案 2 :(得分:0)
没有任何固定的模式将很复杂。 在您的情况下,从逻辑上来说,更好的方法是将日期模式与
这样的文件相关联在相同的逻辑上,获得一个映射表,其中文件名中日期字符串的位置
在宏功能或开关功能中。