creation fichier csv

jujudeo

XLDnaute Nouveau
bonjour a tous,

j'ai un petit soucis au niveau de la formation de mon fichier csv, voici mon code :


Code:
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
 Dim I As Long
 Dim V As Double
 
 '- On enregistre les onglets
 Set f1 = Feuil1: Set f2 = Feuil11: 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
                                         ElseIf Left(c.Value, 1) = "x" Or Left(c.Value, 1) = "X" Or Left(c.Value, 1) = "w" Or Left(c.Value, 1) = "o" Then c.Value = 1
                                            ElseIf Left(c.Value, 1) = "n" Or Left(c.Value, 1) = "N" Then c.Value = 0
 
 
 
                     End If
                 Next c
 
  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
 
          For Each k In dM.Keys: .Range(.Cells(2, k), .Cells(dl, k)).NumberFormat = "0.00": Next k
 
           Columns("A:A").Insert Shift:=xlToLeft
           .Range("A" & .Range("A" & Rows.Count).End(xlUp).Row).Offset(0, 0).Value = "id_dossier"
           For I = 2 To 770
           Cells(I, 1).Value = V + 1
           V = V + 1
          Next
 End With
 
 
 
 '- On réactive les application
 Application.DisplayAlerts = True
 Application.ScreenUpdating = True
 Application.Calculation = xlCalculationAutomatic
 
 
 
 
 
 
          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

Quand je crée mon .csv, je ne comprends pas, les colonnes que j'avais traité pour passer du format date a texte ( 2013-12-11 avec colonne format texte) , se réinitialise et revienne comme a l'état initial(11/12/2013 avec colonne format date, comme si je ne les avait pas traités.

Savez-vous pourquoi cela fait ça et il y a un moyen de contrer sa ?

Je vous remercie par avance

Julien
 

Efgé

XLDnaute Barbatruc
Re : creation fichier csv

Bonjour jujudeo
Un fichier .CSV est un fichier texte, la méthode que tu emploi n'est pas bonne.
Les mises en forme des cellules, des lignes, colonnes, etc.... ne sont pas nécéssaires
Peux tu donner un exemple de ton classeur d'origine ?
Cordialement
 

Efgé

XLDnaute Barbatruc
Re : creation fichier csv

Re
En fait, c'est l'ouverture avec Excel qui transforme automatiquement la date.
Si on regarde le fichier CSV en texte, les dates sont bien en YYYY-MM-DD
En éspérant ne rien avoir oublié dans les conditions diverses et variées :D

Voir code dans le post suivant

Cordialement
 
Dernière édition:

Efgé

XLDnaute Barbatruc
Re : creation fichier csv

Re

Petit bémol corrigé :
VB:
Sub Traitement_2()
Dim i&, j&, Num&
Dim TData As Variant, Tmp As Variant
Dim Chem$, Fic$, TmpTxtRw$, Separ$
Chem = ThisWorkbook.Path 'A adapter
Fic = "Traitement.csv"  'A adapter
Separ = ";" ' a adapter (au cas ou....)
With Feuil2 'A adapter
    TData = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(3).Row, .Cells(1, .Columns.Count).End(1).Column))
End With
Num = FreeFile
Open Chem & "\" & Fic For Output As #Num

For i = LBound(TData, 1) To UBound(TData, 1)
    TmpTxtRw = IIf(i = 1, "id_dossier", i - 1) & Separ
    For j = LBound(TData, 2) To UBound(TData, 2)
        Tmp = TData(i, j)
        
        If Left(Tmp, 1) = "/" Or _
        Tmp = 0 Or _
        InStr(1, Tmp, "€") > 0 Then Tmp = ""
        
        If Mid(Tmp, 3, 1) = "/" And Mid(Tmp, 6, 1) = "/" Then Tmp = CStr(Right(Tmp, 4) & "-" & Mid(Tmp, 4, 2) & "-" & Left(Tmp, 2))
        If Right(Tmp, 1) = "%" Then Tmp = CDbl(Tmp) * 100
        If Left(Tmp, 1) = "N" Then Tmp = 0
        
        If UCase(Left(Tmp, 1)) = "X" Or _
        Left(Tmp, 1) = "o" Or _
        Tmp = "w" Then Tmp = 1
        
        TmpTxtRw = TmpTxtRw & Tmp & Separ
    Next j
    Print #Num, Left(TmpTxtRw, Len(TmpTxtRw) - 1)
Next i
Close Num
End Sub
Cordialement
 
Dernière édition:

Efgé

XLDnaute Barbatruc
Re : creation fichier csv

Re
Toi seul peux le dire....
Vérifies si les résultats sont identiques
Un "vrai" fichier .csv est un fichier texte dont les colonnes sont séparées (en europe) par des ";".
Ouvre le en faisant Clic droit sur le fichier / ouvrir avec / choisir Bloc note et tu verras la véritable structure du fichier sans interprétation pas Excel.
Cordialement
 

Discussions similaires

Réponses
1
Affichages
168

Statistiques des forums

Discussions
312 211
Messages
2 086 300
Membres
103 173
dernier inscrit
Cerba95