Option Explicit
Sub Tst()
Dim Fichier As Variant
ChDrive ThisWorkbook.Path
ChDir ThisWorkbook.Path
If IsEmpty(ShDatas.UsedRange) Then
MsgBox "Feuille Vide : Rien à enregistrer", vbOKOnly + vbInformation, "Info"
Exit Sub
End If
Fichier = Application.GetSaveAsFilename(InitialFileName:="Essai", fileFilter:="Fichier Txt (*.txt), *.txt" )
If Fichier <> False Then Ecrire Fichier
End Sub
Private Sub Ecrire(ByVal NomFichier As String)
Dim Chaine As String
Dim r As Long, c As Integer
Dim NumFichier As Integer
Dim FirstRow As Long, FirstCol As Integer
Dim LastRow As Long, LastCol As Integer
With ShDatas
FirstRow = .Cells.Find("*", .Cells(.Cells.Count), xlFormulas, xlWhole, xlByRows, xlNext).Row
LastRow = .Cells.Find("*", .Cells(.Cells.Count), xlFormulas, xlWhole, xlByRows, xlPrevious).Row
FirstCol = .Cells.Find("*", .Cells(.Cells.Count), xlFormulas, xlWhole, xlByColumns, xlNext).Column
LastCol = .Cells.Find("*", .Cells(.Cells.Count), xlFormulas, xlWhole, xlByColumns, xlPrevious).Column
End With
Close
NumFichier = FreeFile
Open NomFichier For Output As #NumFichier
For r = FirstRow To LastRow
Chaine = ""
For c = FirstCol To LastCol
Chaine = Chaine & Cells(r, c) & String(20 - Len(Cells(r, c)), " " )
Next c
Print #NumFichier, Chaine
Next r
Close #NumFichier
End Sub
|