Option Explicit
Sub ExportCSV()
Dim Ws As Worksheet
Dim iRow As Long, iCol As Long
Dim i As Long, j As Long
Dim Rng As Range
Dim sStr As String, sPath As String
Dim NumFichier As Integer
Const Separateur As String = ";"
Set Ws = ActiveSheet
sPath = ThisWorkbook.Path & "\" & Ws.Name & ".csv"
iCol = Ws.UsedRange.Columns.Count
iRow = Ws.UsedRange.Rows.Count
For i = 1 To iRow
For j = 1 To iCol
Set Rng = Ws.Cells(i, j)
If Rng.NumberFormat = "@" Then
sStr = sStr & Separateur & Rng.Value & _
Separateur & IIf(j < iCol, Separateur, "" )
Else
sStr = sStr & IIf(Rng.NumberFormat <> _
"General", Format(Rng.Value, Rng.NumberFormat), _
Rng.Value) & IIf(j < iCol, Separateur, "" )
End If
Next j
sStr = sStr & IIf(i < iRow, vbCrLf, "" )
Next i
Close
NumFichier = FreeFile
If Len(sStr) > 0 Then
Open sPath For Output As #NumFichier
Print #NumFichier, sStr
Close #NumFichier
Else
MsgBox "Pas de données dans feuille active"
End If
Set Rng = Nothing
Set Ws = Nothing
End Sub
|