0
votes

VBA Convert Date

Qu'est-ce que je fais mal?

in cellule A2 J'ai un format de date (USA) 19790131. J'aimerais avoir cette réécriture dans le 31.01.1979 Entrez le code ici code> mais obtenir le résultat : 1/3/1979 p>

For i = 2 To z
DatText = Cells(i, x)
Cells(i, x) = CDate(Mid(DatText, 7, 2) & "." & Mid(DatText, 5, 2) & "." & Mid(DatText, 1, 4))
Cells(i, x).NumberFormat = "dd.mm.yyyy"
Next i


0 commentaires

3 Réponses :


0
votes

Utilisez des barres obliques au lieu de périodes, donc CDate code> comprendra.

Cells(i, x) = CDate(Mid(DatText, 7, 2) & "/" & Mid(DatText, 5, 2) & "/" & Mid(DatText, 1, 4))


2 commentaires

Merci pour l'indice. Mais ce n'est pas ça.


Impair, car cela a fonctionné parfaitement bien quand je l'ai testé.



0
votes

Si la date est une date "réelle" Excel Date , avec un format de cellule de "yyyymmdd" , vous devez simplement modifier le format de cellule en "dd.mm.yyy"

Si la date est le numéro réel: 19790131 , vous devez la convertir d'abord à une date réelle. Essayez: xxx

puis formater la cellule (NumberFormat) comme "dd.mm.yyy"


0 commentaires

0
votes

Vous pouvez utiliser format em> pour cela - et inclure isdate em> pour une manutention d'erreur:

Public Function ConvertDate()
    
    Dim ThisCell    As Excel.Range
    
    Dim RowIndex    As Long
    Dim ColumnIndex As Long
    Dim DatText     As String
    
    ColumnIndex = 1

    For RowIndex = 2 To 10
        Set ThisCell = Cells(RowIndex, ColumnIndex)
        If Not IsDate(ThisCell.Value) Then
            DatText = Format(ThisCell.Value, "@@@@\/@@\/@@")
            If IsDate(DatText) Then
                ThisCell.Value = CDate(DatText)
            End If
        End If
    Next

End Function


0 commentaires