[VBA Excel] Conversion xls vers csv

Résolu/Fermé
Shooter78 Messages postés 12 Date d'inscription mardi 12 mars 2013 Statut Membre Dernière intervention 13 septembre 2014 - 17 févr. 2014 à 12:41
Shooter78 Messages postés 12 Date d'inscription mardi 12 mars 2013 Statut Membre Dernière intervention 13 septembre 2014 - 17 févr. 2014 à 16:23
Bonjour,
j'ai trouvé ceci sur internet me permettant de convertir des fichiers XLS possédant plusieurs feuilles en CSV.
Le soucis, je voudrais qu'il ne traite qu'une seul feuille (par exemple la feuille toto2)
Comment dois-je m'y prendre ?
Merci d'avance


Sub ConvertXLStoCSV()
On Error GoTo erreur
Application.ScreenUpdating = False
Dim curSheet As Worksheet, csvLine As String, csvSeparator As String, myFso, csvFile, i As Integer, j As Integer
Dim strXLSFile As String, strInputFolder As String, strOutputFolder As String
Dim strCSVFile As String
csvSeparator = ";"

'Change Input and Output folders to relevant location
strInputFolder = ThisWorkbook.Path & "\"
strOutputFolder = ThisWorkbook.Path & "\"
Set myFso = CreateObject("Scripting.FileSystemObject")

strXLSFile = Dir(strInputFolder & "*.xls")

Do While strXLSFile <> ""
If Not strInputFolder & strXLSFile = ThisWorkbook.Path & "\" & ThisWorkbook.Name Then
Workbooks.Open strInputFolder & strXLSFile
For Each curSheet In ActiveWorkbook.Sheets
With curSheet
strCSVFile = Left((strXLSFile), InStrRev(strXLSFile, ".") - 1) & "_" & .Name & ".csv"
Set csvFile = myFso.CreateTextFile(Filename:="C:/users/Administrateur/Desktop/" & strCSVFile, overwrite:=True)
For i = 1 To .Cells(.Rows.Count, 1).End(xlUp).Row
'initialiser la ligne CSV
csvLine = vbNullString
'boucler sur les 4 colonnes
For j = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column
'créer la ligne
csvLine = csvLine & IIf(csvLine = vbNullString, vbNullString, csvSeparator) & .Cells(i, j).Text
Next j
'écrire la ligne dans le fichier
csvFile.WriteLine csvLine
Next i
csvFile.Close
End With
Next curSheet
ActiveWorkbook.Close False
End If
strXLSFile = Dir
Loop
erreur:
Application.ScreenUpdating = True
End Sub
A voir également:

2 réponses

Bonjour,

Remplacer
        For Each curSheet In ActiveWorkbook.Sheets
par
        Set curSheet = ActiveWorkbook.Sheets("toto2")

et supprimer la ligne
        Next curSheet

A+
0
Shooter78 Messages postés 12 Date d'inscription mardi 12 mars 2013 Statut Membre Dernière intervention 13 septembre 2014 3
17 févr. 2014 à 16:23
Merci sa fonctionne,
0