1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| Sub Retraitement_CSV_Dossiers()
Dim pqDestinationCell As Range
Dim cheminFi, nomFi, cheminComplet, dummy, ext, a, b As String
Dim cn As WorkbookConnection, qry As WorkbookQuery
On Error Resume Next
For Each cn In ActiveWorkbook.Connections
cn.Delete
Next cn
For Each qry In ActiveWorkbook.Queries
qry.Delete
Next qry
cheminComplet = Application.GetOpenFilename
If cheminComplet <> "faux" Then
' 1 l'extension
dummy = cheminComplet
While Right(dummy, 1) <> "."
ext = Right(dummy, 1) & ext
dummy = Left(dummy, Len(dummy) - 1)
Wend
dummy = Left(dummy, Len(dummy) - 1)
While Right(dummy, 1) <> "\"
nomFi = Right(dummy, 1) & nomFi
dummy = Left(dummy, Len(dummy) - 1)
Wend
' le chemin
cheminFi = dummy
End If
With ActiveSheet
a = Left(nomFi, 8) & " " & "TOTO" & " " & Right(nomFi, 8)
b = "_" & Left(nomFi, 8) & "_" & "TOTO" & "_" & Right(nomFi, 8)
End With
ActiveWorkbook.Queries.Add Name:=a, Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Csv.Document(File.Contents(""" & cheminComplet & """),[Delimiter=""|"", Columns=34, Encoding=1252, QuoteStyle=QuoteStyle.None])," & Chr(13) & "" & Chr(10) & " #""En-têtes promus"" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])" & _
"," & Chr(13) & "" & Chr(10) & "#""Type modifié"" = Table.TransformColumnTypes(#""En-têtes promus"",{{""Donnée1"", type text}, {""Donnée2"", type text}, {""Donnée3"", type text}, {""Donnée4"", type text}, {""Donnée5"", type text}, {""Donnée6"", type text}, {""Donnée7"", type text}, {""Donnée8"", type datetime}, {""Donnée9"", type datetime}, {""Donnée10"", type text}, {""Do" & _
"nnée11"", type text}, {""Donnée12"", type text}, {""Donnée13"", type text}, {""Donnée14"", type text}, {""Donnée15"", type text}, {""Donnée16"", type text}, {""Donnée17"", type text}, {""Donnée18"", type date}, {""Donnée19"", type text}, {""Donnée20"", type text}, {""Donnée21"", type text}, {""Donnée22"", type text}, {""Donnée23"", Int64.Type}, {""Do" & _
"nnée24"", type text}, {""Donnée25"", type text}, {""Donnée26"", type text}, {""Donnée27"", type text}, {""Donnée28"", type text}, {""Donnée29"", type datetime}, {""Donnée30"", type text}, {""Donnée31"", type text}, {""Donnée32"", type text}, {""Donnée33"", type text}, {" & _
"""Donnée34"", type text}})," & Chr(13) & "" & Chr(10) & " #""Colonnes supprimées"" = Table.RemoveColumns(#""Type modifié"",{""Donnée29"", ""Donnée28"", ""Donnée27"", ""Donnée26""})," & Chr(13) & "" & Chr(10) & " #""Colonnes permutées"" = Table.ReorderColumns(#""Colonnes supprimées"",{""Donnée1"", ""Donnée2"", ""Donnée3"", ""Donnée4"", ""Donnée5"", """ & _
"Donnée6"", ""Donnée7"", ""Donnée8"", ""Donnée9"", ""Donnée10"", ""Donnée11"", ""Donnée12"", ""Donnée13"", ""Donnée14"", ""Donnée15"", ""Donnée16"", ""Donnée17"", ""Donnée18"", ""Donnée19"", ""Donnée20"", ""Donnée21"", ""Donnée22"", ""Donnée23"", ""Donnée24"", ""Donnée25"", ""Donnée26"", ""Donnée27"", ""Don" & _
"née28"", ""Donnée29"", ""Donnée30""})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Colonnes permutées"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""" & a & """;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [" & a & "]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = b
.Refresh BackgroundQuery:=False
End With
ActiveSheet.Select
ActiveSheet.Move
ChDir cheminFi
ActiveWorkbook.SaveAs Filename:=cheminComplet, FileFormat:=xlCSV, CreateBackup:=False
ActiveWindow.Close
End Sub
Le nouveau fichier s'enregistre donc en lieu et place du fichier initial mais avec un nouveau format xlCSV dont le délimiteur est mis automatiquement.
Par la suite ma macro utilise le nouveau fichier CSV + 3 fichiers CSV que je ne modifie pas pour réaliser d'autres traitements :
Sub AjouterDansClasseurExcel(FichierCSV, Classeur As Workbook, NomFeuille As String)
Dim wkbClasseur As Workbook
Set wkbClasseur = Workbooks.Open(FichierCSV, , True, Delimiter:="|", local:=True)
wkbClasseur.ActiveSheet.Copy Classeur.ActiveSheet
Classeur.ActiveSheet.Name = NomFeuille
wkbClasseur.Close False
If NomFeuille = "Dossiers" Then
Classeur.ActiveSheet.Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12 _
, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1), Array(17, 1), Array(18, 1), _
Array(19, 1), Array(20, 1), Array(21, 1), Array(22, 1), Array(23, 1), Array(24, 1), Array( _
25, 1), Array(26, 1), Array(27, 1), Array(28, 1), Array(29, 1), Array(30, 1)), _
TrailingMinusNumbers:=True
ElseIf NomFeuille = "Actions" Then
Classeur.ActiveSheet.Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1)), TrailingMinusNumbers:=True
ElseIf NomFeuille = "Statuts" Then
Classeur.ActiveSheet.Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, _
1)), TrailingMinusNumbers:=True
ElseIf NomFeuille = "Tâches" Then
Classeur.ActiveSheet.Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|", FieldInfo:=Array(Array(1, 1), Array(2, 2), Array(3, 1), Array(4, 1), Array(5, _
1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12 _
, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array(16, 1)), TrailingMinusNumbers:= _
True
Else
Classeur.ActiveSheet.Columns(1).TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="|"
End If
End Sub |