ExportCSV : de feuille active à classeur

manu cho

XLDnaute Nouveau
Bonjour à tous ! ;)


L’excellente macro ci-après permet d’enregistrer la feuille active au format CSV :
Sub ExportCSV()
' Export le contenu d'une feuille au format CSV et l'enregistre automatiquement dans le folder ou est stocké le fichier XLS.
Dim objF As Worksheet
Dim lngCellules As Long
Dim lngColonnes As Long
Dim i As Long
Dim R As Range
Dim j As Long
Dim fCond As FormatCondition
Dim strCSV As String
Dim sPath As String

Set objF = Excel.Active.Sheet

sPath = ThisWorkbook.Path & "\" & objF.Name & ".csv"

lngColonnes = objF.UsedRange.Columns.Count
lngCellules = objF.UsedRange.Rows.Count

For i = 1 To lngCellules
For j = 1 To lngColonnes
Set R = objF.Cells(i, j)
If R.NumberFormat = "@" Then
strCSV = strCSV & Chr(34) & R.Value & _
Chr(34) & IIf(j < lngColonnes, ";", "")
Else
strCSV = strCSV & IIf(R.NumberFormat <> _
"General", Format(R.Value, R.NumberFormat), _
R.Value) & IIf(j < lngColonnes, ";", "")
End If
Next
strCSV = strCSV & IIf(i < lngCellules, vbCrLf, "")
Next

If Len(strCSV) > 0 Then
Open sPath For Output As #1
Print #1, strCSV
Close #1
MsgBox "L'exportation c'est bien déroulé"
Else
MsgBox "Il n'y a aucune donnée dans la feuille active"
End If

Set R = Nothing
Set fCond = Nothing
Set objF = Nothing

End Sub

J’aimerai pouvoir enregistrer toutes les feuilles du classeur, hormis les 3 premières ou 3 noms de feuilles spécifiques. Or, lorsque je remplace : Set objF = Excel.Active.Sheet par Set objF = Excel.Worksheet, cela ne fonctionne pas.
Que fais-je de mal ?

Bonne journée à tous et merci.

Manu.
 

porcinet82

XLDnaute Barbatruc
Re : ExportCSV : de feuille active à classeur

Salut,

Peut etre avec les modif suivantes, mais j'ai pas testé :
Code:
Sub ExportCSV()
' Export le contenu d'une feuille au format CSV et l'enregistre automatiquement dans le folder ou est stocké le fichier XLS.
Dim objF As Worksheet
Dim lngCellules As Long
Dim lngColonnes As Long
Dim i As Long
Dim R As Range
Dim j As Long
Dim fCond As FormatCondition
Dim strCSV As String
Dim sPath As String

For k = 1 To Sheets.Count
    If Sheets(k).Name <> "nom_feuille_1" And Sheets(k).Name <> "nom_feuille_2" And Sheets(k).Name <> "nom_feuille_3" Then
        Set objF = Sheets(k)
        sPath = ThisWorkbook.Path & "\" & objF.Name & ".csv"
        
        lngColonnes = objF.UsedRange.Columns.Count
        lngCellules = objF.UsedRange.Rows.Count
        
        For i = 1 To lngCellules
            For j = 1 To lngColonnes
                Set R = objF.Cells(i, j)
                If R.NumberFormat = "@" Then
                    strCSV = strCSV & Chr(34) & R.Value & Chr(34) & IIf(j < lngColonnes, ";", "")
                Else
                    strCSV = strCSV & IIf(R.NumberFormat <> "General", Format(R.Value, R.NumberFormat), R.Value) & IIf(j < lngColonnes, ";", "")
                End If
            Next
            strCSV = strCSV & IIf(i < lngCellules, vbCrLf, "")
        Next
        
        If Len(strCSV) > 0 Then
            Open sPath For Output As #1
            Print #1, strCSV
            Close #1
            MsgBox "L'exportation c'est bien déroulé"
        Else
            MsgBox "Il n'y a aucune donnée dans la feuille active"
        End If
        
        Set R = Nothing
        Set fCond = Nothing
        Set objF = Nothing
    End If
Next k
End Sub

@+
 

manu cho

XLDnaute Nouveau
Re : ExportCSV : de feuille active à classeur

Merci porcinet82 ! Ce bout de code m'aura permis de mener à bien mon idée.
Il reste maintenant la problèmatique "envoi des fichiers obtenus, aux bons destinaires (1fichier=minimum 1 destinaire).

Thk en tout cas.
ciao
 

Discussions similaires

Statistiques des forums

Discussions
312 496
Messages
2 088 978
Membres
103 996
dernier inscrit
KB4175