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
|
Sub ExportCSV_txt()
Dim noLig As Integer
Dim noCol As Integer
Dim noColDeb As Integer
Dim noColMax As Integer
Dim noLigTitreMax As Integer
Dim noLigTitre As Integer
Dim noColTitre As Integer
Dim ficCSV As String
noLig = 4 ' Première ligne de données
noLigTitreMax = 3 ' Dernière ligne de titre
noColDeb = 3 ' Première colonne de données
noColMax = 6 ' Dernière colonne de données
'
' Pour toutes les lignes à traiter
ficCSV = ""
While Not IsEmpty(Cells(noLig, 1))
'
' Pour toutes les colonnes de connées
noCol = noColDeb
While noCol <= noColMax
ficCSV = ficCSV & Cells(noLig, 1) & ";" & Cells(noLig, 2)
ficCSV = ficCSV & ";" & Cells(noLig, noCol)
'
' Pour toutes les lignes de titre
noLigTitre = noLigTitreMax
While noLigTitre <> 0
'
' Recherche du titre
noColTitre = noCol
Do
'
' Jusqu'à la colonne contenant le titre
If Not IsEmpty(Cells(noLigTitre, noColTitre)) Then
ficCSV = ficCSV & ";" & Cells(noLigTitre, noColTitre)
Exit Do
Else
noColTitre = noColTitre - 1
End If
Loop
noLigTitre = noLigTitre - 1
Wend
ficCSV = ficCSV & vbCrLf
noCol = noCol + 1
Wend
'
noLig = noLig + 1
Wend
'
'-- Construction du nom du fichier CSV
'--------------------------------------
DossierFichierExcel = ActiveWorkbook.Path
DateSystème = Date
DateSSAAMMJJ = Mid(DateSystème, 7, 4) & Mid(DateSystème, 4, 2) & Mid(DateSystème, 1, 2)
NomFichierCSV = "Import_Droits_Dossiers_" & Range("C2").Value
NomFichierCSV = NomFichierCSV & "_" & DateSSAAMMJJ & ".CSV"
'-- Création du fichier CSV
'---------------------------
Separateur = ";"
ThePath = ThisWorkbook.Path & "\" & NomFichierCSV
TheFile = Application.GetSaveAsFilename(ThePath, "CSV ,*.csv")
If TheFile = False Then Exit Sub
Open TheFile For Output As #1
'Open DossierFichierExcel & "\" & NomFichierCSV For Output As #1
Print #1, ficCSV '= " "
Close
End Sub |
Partager