Microsoft 365 Rendre une macro plus flexible

netparty

XLDnaute Occasionnel
Bonjour à tous

J'ai une macro qui me sert à exporter mon tableau vers un fichier CSV, mais j'aimerais la rendre plus flexible,
Pour l'instant la macro exporter les plages A2 à K mais mon tableau pourrais avoir moins de colonne ou plus de colonne du fait je dois modifier ma macro pour prendre en compte la bonne plage du tableau.

Est-il possible que la plage de départ soit toujours A2 mais que la plage de fin soit la dernier colonne.

VB:
Sub ExportCsv()
Dim Plage As Object, oL As Object, oC As Object
Dim Tmp$, Sep$
Dim Fichier$, Chemin$, CheminFiche$, Nlig&
   With Application
      .ScreenUpdating = False
      .EnableEvents = False
      .Calculation = xlManual
   End With
'Fichier = "Base" & ".csv"
Fichier = "Références matériels" & ".csv"

Chemin = ActiveWorkbook.Path & Application.PathSeparator
CheminFiche = Chemin & Fichier
Nlig = Cells(Rows.Count, 1).End(xlUp).Row
Sep = ","
   Set Plage = Range("A2:K" & Nlig)
      Open CheminFiche For Output As #1
         For Each oL In Plage.Rows
            Tmp = ""
               For Each oC In oL.Cells
                  Tmp = Tmp & CStr(oC.Text) & Sep
               Next
            Print #1, Left(Tmp, Len(Tmp) - 1)
         Next
      Close
   Set Plage = Nothing
MsgBox "Export Base Terminer", vbInformation, "Admin"
    With Application
       .ScreenUpdating = True
       .Calculation = xlCalculationAutomatic
       .EnableEvents = True
       .Goto [A1], True
    End With
End Sub

Merci d'avance
 

Staple1600

XLDnaute Barbatruc
Re


Essaie ainsi
Code:
Sub Export_CSV_light_bis()
Dim fCSV As Workbook, strFichier$
With ThisWorkbook
strFichier = .Path & "\" & Split(.Name, ".")(0) & ".csv"
End With
Application.ScreenUpdating = 0: Application.DisplayAlerts = 0
ActiveSheet.Copy:Set fCSV = ActiveWorkbook
With fCSV
.Sheets(1).Rows("1:2").Delete
.SaveAs Filename:=strFichier, FileFormat:=6, Local:=-1: .Close 0
End With
End Sub
NB: Ou selon le besoin
.Sheets(1).Rows("1:3").Delete
 

netparty

XLDnaute Occasionnel
Re


Essaie ainsi
Code:
Sub Export_CSV_light_bis()
Dim fCSV As Workbook, strFichier$
With ThisWorkbook
strFichier = .Path & "\" & Split(.Name, ".")(0) & ".csv"
End With
Application.ScreenUpdating = 0: Application.DisplayAlerts = 0
ActiveSheet.Copy:Set fCSV = ActiveWorkbook
With fCSV
.Sheets(1).Rows("1:2").Delete
.SaveAs Filename:=strFichier, FileFormat:=6, Local:=-1: .Close 0
End With
End Sub
NB: Ou selon le besoin
.Sheets(1).Rows("1:3").Delete
Merci c'est nickel
 

Discussions similaires

Réponses
4
Affichages
1 K