Ich bin noch neu in VBA, dieses Problem verwirrt mich ein wenig. Ich schreibe ein Makro, das Excel-Tabellen formatiert, die ich regelmäßig bekomme.
Das Standarddatum, das ich in dieser Tabelle habe, wird als Text gespeichert. Ändern Sie es über die Benutzeroberfläche und zeichnen Sie auf, dass ein Makro die Daten im Autofilter nicht erkennt.
Es sieht so aus
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
Dies funktioniert teilweise, bricht aber aus irgendeinem Grund in der Hälfte der Monate wie März, Oktober usw. Wenn ich auf die Zelle doppelklicke, um sie zu bearbeiten, und die Eingabetaste drücke, wird das Datum korrekt gespeichert und vom Autofilter erkannt.
Wie behebe ich das?
Bearbeiten: Beispiel dafür, wie es in der Tabelle aussieht:
Lösung des Problems
Stellen Sie sich die folgenden Texte in A1:A13 vor, die wir in reelle numerische Daten umwandeln möchten
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
Und das Ergebnis wird sein
Warum haben wir das getan If Format$(RetVal, "DD.MM.YYYY") <> DateString Then
?
Denn wenn Ihre Saiten "falsche" Daten 35.01.2022
haben, müssen wir sie erkennen. Denn DateSerial
würde es einfach in das Datum umwandeln, 04.02.2022
weil der Tag 35
im Januar nicht existiert.
Keine Kommentare:
Kommentar veröffentlichen