日期(字符串)未正确更改为日期

Date (string) not changed properly into date

我还是新手 VBA 这个问题让我有些困惑。 我正在编写一个宏来格式化 excel 我经常得到的电子表格。

我在此电子表格中的默认日期保存为文本。 通过 UI 更改它并将其记录为宏无法识别自动过滤器中的日期。

看起来像

    Dim i As Integer
    i = Cells.Find(What:="*", _
                    After:=Range("C1"), _
                    LookAt:=xlPart, _
                    LookIn:=xlFormulas, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False).Row
    Dim k As Integer
    
    For k = 3 To i
        Cells(k, 3) = Format(Cells(k, 3), "dd mmmm yyyy;@")
        ' Changing the format like dd.mm.yyyy;@ doesn't work at all 
    Next k

这确实部分有效,但由于某些原因在三月、十月等半月中断。当我双击单元格进行编辑并点击输入时,日期被正确保存并被自动过滤器识别。

我该如何解决这个问题?

编辑:电子表格内部的示例:

假设我们要将 A1:A13 中的以下文本转换为实际数字日期

Option Explicit

Public Sub Example()
    Dim RangeToConvert As Range
    Set RangeToConvert = Range("A1:A13")  ' define the range of texts DD.MM.YYYY you want to convert to real numeric dates
    
    ' read them into an array for faster processing
    Dim Data() As Variant
    Data = RangeToConvert.Value2
    
    ' convert all texts to dates in that array
    Dim iRow As Long
    For iRow = LBound(Data, 1) To UBound(Data, 1)
        Data(iRow, 1) = ConvertTextDDMMYYYYtoDate(Data(iRow, 1))
    Next iRow
    
    ' write the real numerc dates back to the cells
    RangeToConvert.Value2 = Data
    
    ' format the date to whatever you like
    RangeToConvert.NumberFormat = "DD. MMM YYYY" 'however you want it to look like
End Sub


Public Function ConvertTextDDMMYYYYtoDate(ByVal DateString As String) As Date
    Dim Parts() As String
    Parts = Split(DateString, ".")  ' split date 13.01.2022 into 3 parts
    
    Dim RetVal As Date
    
    If UBound(Parts) = 2 Then ' check if there were 3 parts in the text if not it's the wrong format in the DateString 
        RetVal = DateSerial(Parts(2), Parts(1), Parts(0))  ' put the 3 parts together to a numeric date
        
        ' check if the numeric date is the same as the DateString we had as input
        If Format$(RetVal, "DD.MM.YYYY") <> DateString Then
            ' if that is not the case it means the string was no valid date and cannot be converted
            MsgBox """" & DateString & """ is not a valid date in the format TT.MM.JJJJ"
            Exit Function
        End If
    Else
        MsgBox """" & DateString & """ is not in the format TT.MM.JJJJ"
        Exit Function
    End If
    
    ' return the value as real numeric date
    ConvertTextDDMMYYYYtoDate = RetVal
End Function

结果会是

我们为什么要这样做If Format$(RetVal, "DD.MM.YYYY") <> DateString Then
因为如果您的字符串是 35.01.2022 之类的“错误”日期,我们需要检测帽子。因为 DateSerial 只会将其转换为日期 04.02.2022,因为 day 35 在 1 月不存在。


根据评论编辑

如果您有多个列,您还需要一个循环遍历这些列的第二个循环。

Dim iCol As Long
For iCol = LBound(Data, 2) To UBound(Data, 2)
    Dim iRow As Long
    For iRow = LBound(Data, 1) To UBound(Data, 1)
        Data(iRow, iCol ) = ConvertTextDDMMYYYYtoDate(Data(iRow, iCol ))
    Next iRow
Next iCol