1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| Private Sub Export_fic_txt_CommandButton1_Click()
Dim oFSO As Scripting.FileSystemObject
Dim oTxt As Scripting.TextStream
'Instanciation du FSO
Set oFSO = New Scripting.FileSystemObject
Set oTxt = oFSO.CreateTextFile("C:\Users\p_bez\ExportListe_Listbox.txt", True) 'Le True ecrase le fichier si il existe déjà !!!!
Application.ScreenUpdating = True
Dim lig_num As Long
Dim Last_Row As Long
Dim i As Integer
Dim Plage
Set Feuil = Worksheets("RegistreTemp")
Last_Row = Feuil.[A65000].End(xlUp).row 'dernière ligne non vide de la feuille concernée
'CONFIGURATION 1 : avec l'utilisation d'une plage là j'ai abandonné, trop difficile !!!
Plage = Feuil.Range("A1:H" & Feuil.[A65000].End(xlUp).row).Value 'Feuil.[A65000].End(xlUp).Row donne le dernier numéro de ligne renseigné
i = 1
With oTxt
For lig_num = 1 To Worksheets("RegistreTemp").Rows.Count
If i > Last_Row Then
Exit For
End If
'CONFIGURATION 2 : avec l'utilisation de Rows
.WriteLine Worksheets("RegistreTemp").Rows(i).Text
'=> ici je prends Erreur d'exécution "94" : utilisation incorrecte de NUll
'OU
'CONFIGURATION 3 : avec l'utilisation de Range directement
.WriteLine Range(Cells(i, 1), Cells(i, 8)).Text
'=> ici je prends Erreur d'exécution "94" : utilisation incorrecte de NUll
i = i + 1
Next
End With
'--------------------------------------------------------------
i = 1
With oTxt
Dim lig As Range
For Each lig In Worksheets("RegistreTemp").Rows
If i > Last_Row Then
Exit For
End If
'CONFIGURATION 4 : avec l'utilisation d'un élément Range
.WriteLine lig(Cells(i, 1), Cells(i, 8)).Text
'=> Erreur d 'exécution '1004" : Erreur définie par l'application ou par l'objet
i = i + 1
Next
End With
'---------------------------------------------------------------
'Il ne faut absolument pas oublier de fermer le fichier
oTxt.Close
Application.ScreenUpdating = False
End Sub |
Partager