Option Explicit
Const oldChaine As String = "Mois,Lieu,Article,Quantités,Montant,Code"
Const NewChaine As String = "Date,Ville,Marchandise,Quantités,Vente,Référence"
Sub choix_dossier()
Dim Dossier
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then Dossier = .SelectedItems(1)
If Dossier = "" Then Exit Sub
End With
replaceAllCsvHeader Dossier
MsgBox " Remplacement du Header dans les CSVs terminé"
End Sub
Function replaceAllCsvHeader(Dossier)
Dim FsO As Object, fich, x&, subdossier, contenu As String
Set FsO = CreateObject("Scripting.FileSystemObject")
Set Dossier = FsO.GetFolder(Dossier)
For Each fich In Dossier.Files
'lecture
With CreateObject("ADODB.Stream")
.Charset = "utf-8": .Open: .LoadFromFile (fich): contenu = .ReadText()
End With
'ecriture
x = FreeFile: Open fich For Output As #x: Print #x, Replace(contenu, oldChaine, NewChaine): Close #x
Next
For Each subdossier In Dossier.subfolders
replaceAllCsvHeader subdossier.Path
Next subdossier
Set FsO = Nothing
End Function