Txt en Excel avec VBA
g
Bonjour,
J'aimerais transformer des fichier txt en excel en tout téléchargeant le fichier
J'ai essayé ce code mais ça bugue.
Cordialement,
g
Dim ligne_debut As Integer: Dim colonne_debut As Integer
Dim ligne_fin As Integer: Dim colonne_fin As Integer
Dim ligne_enCours As Integer: Dim colonne_enCours As Integer
Private Sub importer_Click()
Dim fichier_choisi As String
fichier_choisi = Application.GetOpenFilename(" Text Files (*.txt), *.txt", , "selectionner le fichier choisi CSV")
If (LCase(fichier_choisi) <> "Faux" And fichier_choisi <> "0") Then
Listfichier.AddItem (fichier_choisi)
End If
End Sub
Private Sub Exporter_Click()
Dim nom_fichier As String
ligne_debut = 1: colonne_debut = 1
ligne_enCours = ligne_debut: colonne_enCours = colonne_debut
Cells.Clear
For i = 0 To Listfichier.ListCount - 1
lecture (Listfichier.List(i))
Next i
traitement
nom_fichier = Application.GetSaveAsFilename(filefilter:="Text Files (*.txt), *.txt")
Sortie.Value = nom_fichier
Ecriture (nom_fichier)
End Sub
Private Sub Fermer_Click()
Listfichier.Clear
Formulaire.Hide
End Sub
Private Sub lecture(fichier As String)
Dim depart As Integer, position As Integer
Dim texte As String, tampon As String
Open fichier For Input As #1
Do While Not EOF(1)
Line Input #1, texte
depart = 1: position = 1
Do While (position <> 0)
position = InStr(depart, texte, ";", 1)
If position = 0 Then
tampon = Mid(texte, depart)
Sheets("Feuil1").Cells(ligne_enCours, colonne_enCours).Value = tampon
Exit Do
Else
tampon = Mid(texte, depart, position - depart)
End If
Sheets("Feuil1").Cells(ligne_enCours, colonne_enCours).Value = tampon
depart = position + 1
colonne_enCours = colonne_enCours + 1
Loop
colonne_enCours = colonne_debut
ligne_enCours = ligne_enCours + 1
Loop
Close #1
End Sub
Private Sub Ecriture(fichier As String)
End Sub
Private Sub traitement() 'fichier As String
End Sub#RS
a
Bonjour
voila une macro import TxT
Private Sub ChoixFicTxt()
Dim dossier As FileDialog
ChoixChemin = ActiveWorkbook.Path & Application.PathSeparator
Set dossier = Application.FileDialog(msoFileDialogFilePicker)
With dossier
.AllowMultiSelect = False
.InitialFileName = ChoixChemin
.Title = "Choix d'un fichier TXT"
.Filters.Clear
.Filters.Add "Fichier Csv ", "*.txt*", 1
If .Show = -1 Then
Chemin = .SelectedItems(1)
LireMan Chemin
End If
End With
Set dossier = Nothing
End Sub
Sub LireMan(NomFichier)
Dim Ar() As String
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlManual
End With
Rows("1:" & Rows.Count).Clear
Sep = vbTab
Lig = 1
' -----------------------------------------
On Error Resume Next
Open NomFichier For Input As #1
Do While Not EOF(1)
Line Input #1, Chaine
Ar = Split(Chaine, Sep)
Col = 1
For X = LBound(Ar) To UBound(Ar)
Tmp = Application.Trim(Ar(X))
If IsNumeric(Tmp) Then
Cells(Lig, Col).Value = CDbl(Tmp)
Else
Cells(Lig, Col).Value = Tmp
End If
Col = Col + 1
Next
Lig = Lig + 1
Loop
Close #1
' -----------------------------------------
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.CutCopyMode = False
.Goto [A1], True
End With
End SubA+
Maurice
g
Bonjour Maurice
Merci beaucoup pour votre code qui marche nickel.
Votre aide me fait gagner du temps
Merci