conversion format date en texte en conservant un format de date

jujudeo

XLDnaute Nouveau
Bonjour,

J'ai un petit soucis au niveau de mon format de date, j'aimerais transformer mon format de date en texte/standard tout en gardant une écriture sous la forme : "yyyy-mm-dd"

Code:
Sub Traitement()

 Dim derligne As Long
 Dim x As Range 'cellule affichant le coefficient multiplicateur 100
 Dim taille As Range '1ère ligne contenant le symbole % dans les en-tête
 Dim colonne As Integer 'n° de la colonne à modifier
 Dim lignefin As Integer 'n° de la dernière ligne
 Dim ws As Worksheet
 
 On Error Resume Next 'si la feuille n'existe pas !
 Application.DisplayAlerts = False: Sheets("traitement").Delete: Application.DisplayAlerts = True
 On Error GoTo 0 'plus de gestionnaire d'erreurs
 Worksheets("PO - PB").Copy After:=Worksheets("base donnee") 'création de la feuille
 ActiveSheet.Name = "traitement" 'nom de la feuille'
 Sheets("base donnee").Range("A1:DX1").Copy Sheets("traitement").Range("A1:DX1")
 ActiveSheet.AutoFilterMode = False 'desactiver les filtres'
 ActiveWindow.FreezePanes = False 'désactiver les volets'
 ligne = Range("A" & Rows.Count).End(xlUp).Row
 colomne = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
 With Sheets("traitement")
      Set x = Range("A" & ligne + 10)
           x.Value = 100
           
          Set taille = .Range("A2:DX100")
          For Each cell In taille
          
              If cell.HasFormula = True Then
              cell.EntireColumn.Rows("2:775").Select
              selection.Copy
              selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
          
          
              [b]'detecter date et la mettre en texte + bon format
              ElseIf IsDate(cell) Then
              cell.EntireColumn.Rows("2:761").Select
              selection.NumberFormat = "@"
              selection.NumberFormat = "yyyy-mm-dd"[/b]
                            
             ElseIf InStr(1, cell.Text, "€") > 0 Then
              cell.EntireColumn.Rows("2:761").Select
              selection.NumberFormat = "0.00"  'pour 2 décimales
              selection.Copy
              selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone

             ElseIf InStr(1, cell.Text, "%") > 0 Then
               colonne = cell.Column
               lignefin = cell.End(xlDown).Row
               x.Copy
               Range(Cells(1, colonne), Cells(lignefin, colonne)).PasteSpecial _
               Paste:=xlPasteValues, Operation:=xlMultiply, SkipBlanks:=True
                   For i = 2 To lignefin
                     If CStr(Cells(i, colonne)) = "Erreur 2015" Then Cells(i, colonne) = ""
                   Next i
               selection.NumberFormat = "0.00"
               selection.Copy
              selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
            
             End If
            Next
     End With
     
  ActiveSheet.UsedRange.Replace What:="/", Replacement:="", LookAt:=xlWhole
  ActiveSheet.UsedRange.Replace What:="#REF!", Replacement:="", LookAt:=xlWhole
  ActiveSheet.UsedRange.Replace What:="#VALEUR!", Replacement:="", LookAt:=xlWhole
  
         'For Each ws In Worksheets
         'Application.DisplayAlerts = False
         'If ws.Name <> "traitement" Then ws.Delete
         'Next
          'Application.DisplayAlerts = True
     
'ActiveWorkbook.SaveAs Filename:="Traitement", FileFormat:=xlCSV, CreateBackup:=False, local:=True

 End Sub

Voici ma macro, j'arrive à le transformer mais à la fin, le format de ma cellule est personnalisé, et moi je voudrais texte/standard...

Je vous remercie par avance

Julien
 

Pièces jointes

  • suivi.xlsx
    33.1 KB · Affichages: 37
  • suivi.xlsx
    33.1 KB · Affichages: 33

thebenoit59

XLDnaute Accro
Re : conversion format date en texte en conservant un format de date

Bonjour jujudeo.
Tu peux adapter ton code ainsi :
Code:
'detecter date et la mettre en texte + bon format
              ElseIf IsDate(cell) Then
              cell.EntireColumn.Rows("2:761").Select
              Selection.NumberFormat = "@"
              'Selection.NumberFormat = "yyyy-mm-dd"
              For Each c In Range(Cells(2, cell.Column), Cells(761, cell.Column))
                If c.Value <> "" Then c.Value = Format(c.Value, "yyyy-mm-dd")
            Next c
            Selection.NumberFormat = "General"
Je trouve ça un peu lourd mais tu obtiens le résultat escompté.
 

thebenoit59

XLDnaute Accro
Re : conversion format date en texte en conservant un format de date

Je te propose une autre solution, voir le fichier joint dont le module 1 correspond au code ci-dessous.
Je suis reparti de zéro. Le résultat s'affiche bien plus rapidement et correctement.

Code:
Option Explicit
Sub Traitement()

'- Déclaration des variables
Dim dl As Integer, dc As Integer, lf As Integer
Dim r As Range
Dim f1 As Worksheet, f2 As Worksheet, f3 As Worksheet, f4 As Worksheet
Dim c As Variant, k As Variant
Dim dD As Object, dM As Object, dP As Object
Dim T As Long

'- On enregistre les onglets
Set f1 = Feuil1: Set f2 = Feuil12: Set f3 = Feuil2

'- On désactive les application
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'- On débute la procédure
On Error Resume Next
Worksheets("traitement").Delete
'On Error GoTo 0

'- Copie de la feuille PO-PB en dernière position et renomme par traitement
f1.Copy After:=Worksheets(Worksheets.Count)
Set f4 = Worksheets(Worksheets.Count)
f4.Name = "traitement"

'- On copie la ligne 1 de la bdd
With f2
    .Range(.Cells(1, 1), .Cells(1, .Cells.Find("*", , , , xlByColumns, xlPrevious).Column)).Copy f4.Cells(1, 1)
End With

'- On joue avec la feuille traitement (f4)
With f4
    .Activate
    ActiveWindow.FreezePanes = False
    With .Rows("1")
        .EntireRow.AutoFit
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
    End With
    .AutoFilterMode = False
    dl = .Cells.Find("*", , , , xlByRows, xlPrevious).Row
    dc = .Cells.Find("*", , , , xlByColumns, xlPrevious).Column

    Set r = .Range(.Cells(2, 1), .Cells(dl, dc))
        r.Copy: .Cells(2, 1).PasteSpecial , Paste:=xlPasteValues: Application.CutCopyMode = False
            Set dD = CreateObject("Scripting.Dictionary"): Set dM = CreateObject("Scripting.Dictionary"): Set dP = CreateObject("Scripting.Dictionary")
                For Each c In r
                    If Left(c.Value, 1) = "/" Or c.Value = 0 Then
                        c.ClearContents
                            ElseIf IsDate(c) Then dD(c.Column) = ""
                                ElseIf InStr(1, c.Text, "€") > 0 Then dM(c.Column) = ""
                                    ElseIf Right(c.Text, 1) = "%" Then dM(c.Column) = "": c.Value = c.Value * 100
                    End If
                Next c
            
        For Each k In dD.Keys
            .Range(.Cells(2, k), .Cells(dl, k)).NumberFormat = "@"
            For Each c In .Range(.Cells(2, k), .Cells(dl, k))
                If c.Value <> "" Then c.Value = Format(c.Value, "yyyy-mm-dd")
            Next c
            .Range(.Cells(2, k), .Cells(dl, k)).NumberFormat = "General"
        Next k
            
        For Each k In dM.Keys: .Range(.Cells(2, k), .Cells(dl, k)).Style = "Comma": Next k
End With

'- On réactive les application
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
 

Pièces jointes

  • Jujudeo.xlsm
    51.7 KB · Affichages: 50

jujudeo

XLDnaute Nouveau
Re : conversion format date en texte en conservant un format de date

je te tiens au courant, je regarde si par rapport a mon fichier d'origine sa n'a pas bougé, mais je crois que c'est bon :) merci beaucoup vraiment ! en plus c'est en bonus c'est rapide ;)
 

jujudeo

XLDnaute Nouveau
Re : conversion format date en texte en conservant un format de date

euh :/ les colonne genre monaitaire et % sont en format comptabilité, possible de les passer en format numéro ou standard ?? car le soucis c'est que comme je suis vraiment début en vba cela fais même pas 2 semaines que j'ai commencé, pour modifier ton code je vais avoir un peu de mal car je risque d'identifier d'autre problème au cour de la comparaison
 
Dernière modification par un modérateur:

jujudeo

XLDnaute Nouveau
Re : conversion format date en texte en conservant un format de date

je retire ce que j'ai dit au post #12, j'avais mal fait la modif sa marche très bien ! je viens de me rendre compte que certaines colonnes ont mal été remplies par la personne au niveau de la colonne des dates : 01/0/2013 ou encore 02/08/20145 des exemples comme ceux-la. Est-ce que tu penses qu'il est possible de supprimer / remplacer par du vide dans ces colonnes la, les cellules qui n'ont pas une vrai valeur de date ?
 

thebenoit59

XLDnaute Accro
Re : conversion format date en texte en conservant un format de date

Tu peux essayer :
Code:
For Each k In dD.Keys
            For Each c In .Range(.Cells(2, k), .Cells(dl, k))
                If c.Value <> "" And Not IsDate(c.Value) Then c.ClearContents
            Next c
            .Range(.Cells(2, k), .Cells(dl, k)).NumberFormat = "@"
            For Each c In .Range(.Cells(2, k), .Cells(dl, k))
                If c.Value <> "" Then c.Value = Format(c.Value, "yyyy-mm-dd")
            Next c
            .Range(.Cells(2, k), .Cells(dl, k)).NumberFormat = "General"
        Next k
 

Discussions similaires

Réponses
2
Affichages
148
Réponses
3
Affichages
577

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 206
Messages
2 086 207
Membres
103 157
dernier inscrit
youma