[EXCEL VBA] ::: Exportation d'une feuille excel au format .csv

Résolu
CATDEV Messages postés 8 Date d'inscription samedi 28 octobre 2006 Statut Membre Dernière intervention 4 février 2007 - 5 déc. 2006 à 10:35
CATDEV Messages postés 8 Date d'inscription samedi 28 octobre 2006 Statut Membre Dernière intervention 4 février 2007 - 5 déc. 2006 à 12:27
Bonjour à tout le monde, 



J'ai une petite question, enfin deux 



1 - J'ai un bouton sur une feuille (1) et j'aimerais qu'il permette d'exporter la feuille (5) au format .csv. Comment procéder ?



2 - Quand on exporte une partie d'un fichier Excel en .csv par exemple 
Si je veux faire un bouton pour sauvegarder le fichier Excel, celui-ci s'enregistre en monfichier.csv (monfichier est différent du nom d'origine du fichier Excel, ici c'est ES-v01.0.xls); comment faire un save qui permet de toujours écraser sous le nom de fichier Excel ouvert à l'origine?



J'ai commencé à écrire du code mais je ne sais pas comment finir.



De plus le bout de code pour exporter à la fin de mon traitement, ne fonctionne pas.



[CODE]
' Export the document in cvs file format
' Macro recorded 28/10/2006 by Patrick DUBERNET
'
Public Sub ExportCsv()
           
            Dim LigneIn As String
            Dim LigneExcel As Integer
           
            ' récupération du nombre de fichier à traiter et le nombre de filtres -------
            Call countInFiles
 
            LigneExcel = 2
           
            'Inscrire le contenu d'une feuille Excel dans une autre ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
       
            LigneIn = Sheets("Result").Cells(LigneExcel, 3)
           
            If LigneIn = "" Then
                    MsgBox " Not Data transfered, the entry list is empty ! ", vbCritical, "Caution"
                    Exit Sub
                Else
            End If
           
            For LigneExcel = 2 To frmGenerator.nbrEntries
          
            Sheets("Export.csv").Cells(LigneExcel - 1, 1) = Sheets("Result").Cells(LigneExcel, 1)
            Sheets("Export.csv").Cells(LigneExcel - 1, 2) = CStr(";")
            Sheets("Export.csv").Cells(LigneExcel - 1, 3) = Sheets("Result").Cells(LigneExcel, 3)
           
            Next LigneExcel
           
'export le fichier
'Sheets("Export.csv").Activate
'
'ActiveWorkbook.SaveAs Filename:=Sheets("Memory").Cells(2, 4) & "\referenceFile.csv", FileFormat:=xlCSV, CreateBackup:=False
'
'Sheets("Reference File GENERATOR").Activate
 
            Dim CSVFile As New OLEObject
            On Error Resume Next
            With CSVFile
            .ExportRange = Sheets("Export.csv").Cells
            .Export CSVFileName:=ThisWorkbook.Path & "\referenceFile.csv"
            If Err <> 0 Then MsgBox "Cannot export" & ThisWorkbook.Path & "\referenceFile.csv"
            End With
 
MsgBox "save document under CSV format(referenceFile.csv)", vbExclamation, "Export information message"
End Sub
 [\CODE]



Quelqu'un peut-il m'aider?
cordialement,
Patrick

3 réponses

CATDEV Messages postés 8 Date d'inscription samedi 28 octobre 2006 Statut Membre Dernière intervention 4 février 2007
5 déc. 2006 à 12:27
Merci Drikce 06

j'ai fini et je te remercie.




<hr />




' Export the document in csv file format
' Macro recorded 28/10/2006 by Patrick DUBERNET
'
Public Sub ExportCsv()
           
            Dim LigneIn As String
            Dim LigneExcel As Integer
           
            ' récupération du nombre de fichier à traiter et le nombre de filtres -------
            Call countInFiles



            LigneExcel = 2
           
            'Inscrire le contenu d'une feuille Excel dans une autre ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
       
            LigneIn = Sheets("Result").Cells(LigneExcel, 3)
           
            If LigneIn = "" Then
                    MsgBox " Not Data transfered, the entry list is empty ! ", vbCritical, "Caution"
                    Exit Sub
                Else
            End If
           
            For LigneExcel = 2 To frmGenerator.nbrEntries
          
            Sheets("referenceFile").Cells(LigneExcel - 1, 1) = Sheets("Result").Cells(LigneExcel, 1)
            Sheets("referenceFile").Cells(LigneExcel - 1, 2) = Sheets("Result").Cells(LigneExcel, 3)
           
            Next LigneExcel



Sheets("Memory").Cells(2, 5) = ActiveWorkbook.FullName
'exporte le fichier ------------------------------------------------------------------------



Sheets("referenceFile").SaveAs Filename:=Sheets("Memory").Cells(2, 4) & "\referenceFile.csv", FileFormat:=xlCSV, CreateBackup:=False



'sauvegarde le fichier excel tel quel -----------------------------------------------------



ActiveWorkbook.SaveAs Filename:=Sheets("Memory").Cells(2, 5), FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False



MsgBox "save document under CSV format(referenceFile.csv)", vbExclamation, "Export information message"
End Sub
<hr />


@ bientôt.
Patrick
3
CATDEV Messages postés 8 Date d'inscription samedi 28 octobre 2006 Statut Membre Dernière intervention 4 février 2007
5 déc. 2006 à 11:34
Voilà j'ai modifié mon code :

<hr />
' Export the document in cvs file format
' Macro recorded 28/10/2006 by Patrick DUBERNET
'
Public Sub ExportCsv()
           
            Dim LigneIn As String
            Dim LigneExcel As Integer
           
            ' récupération du nombre de fichier à traiter et le nombre de filtres -------
            Call countInFiles



            LigneExcel = 2
           
            'Inscrire le contenu d'une feuille Excel dans une autre ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
       
            LigneIn = Sheets("Result").Cells(LigneExcel, 3)
           
            If LigneIn = "" Then
                    MsgBox " Not Data transfered, the entry list is empty ! ", vbCritical, "Caution"
                    Exit Sub
                Else
            End If
           
            For LigneExcel = 2 To frmGenerator.nbrEntries
          
            Sheets("referenceFile").Cells(LigneExcel - 1, 1) = Sheets("Result").Cells(LigneExcel, 1)
            Sheets("referenceFile").Cells(LigneExcel - 1, 2) = Sheets("Result").Cells(LigneExcel, 3)
           
            Next LigneExcel
           
'export le fichier



Sheets("referenceFile").SaveAs Filename:=Sheets("Memory").Cells(2, 4) & "\referenceFile.csv", FileFormat:=xlCSV, CreateBackup:=False



MsgBox "save document under CSV format(referenceFile.csv)", vbExclamation, "Export information message"
End Sub
<hr />

Mais comment faire pour que le fichier ouvert rest le nom de fichier Excel ouvert et non pas le nom de fichier sauvegardé ici ES-01.xls devient referenceFile.csv ce qui est normal puisque que je fais un "save as" de la dernière feuille mais comment y remédier?

Cordialement,
Patrick
0
drikce06 Messages postés 2236 Date d'inscription lundi 29 mai 2006 Statut Membre Dernière intervention 29 mai 2008 10
5 déc. 2006 à 11:56
Salut essaye ça:

Dim nom As String
Dim splitnom() As String




nom = ThisWorkbook.Name
splitnom = Split(nom, ".")
nom = splitnom(0)

Sheets("referenceFile").SaveAs Filename:=Application.path  & "" & nom & ".csv", FileFormat:=xlCSV, CreateBackup:=False





 Drikce 06

Si la réponse vous convient: Réponse acceptée. Si la réponse vous convient pas:
0
Rejoignez-nous