Import d'un fichier txt par macro

thierry_44

XLDnaute Junior
Bonjour

comment importer un fichier txt par macro.
Dans l'exemple joint import des datas txt dans la feuille Bil .
structure des données importer :

Fichiers pages date heure

Merci d'avance

thierry44
 

Pièces jointes

  • Tempost.zip
    22.4 KB · Affichages: 47
  • Tempost.zip
    22.4 KB · Affichages: 42
  • Tempost.zip
    22.4 KB · Affichages: 46

_Thierry

XLDnaute Barbatruc
Repose en paix
Re : Import d'un fichier txt par macro

Bonjour Thierry, Skoobi, le Forum

Oui en effet par l'enregistreur de Macro on peut obtenir un résultat.

Sinon avec un Statement Open on peut lire aussi le fichier TXT comme suit :

Code:
Option Explicit

Sub ImportTXT()
Dim Record As String
Dim Container As Variant
Dim i As Integer, ii As Byte
Dim ThePath As String
 
ThePath = ThisWorkbook.Path & "\tempost.txt" 'A Adapter...
Open ThePath For Input As #1
    
    Do While Not EOF(1)
        i = i + 1
        Line Input #1, Record
        Container = Split(Record, Chr(9))
            
            
                For ii = 1 To 3
                
                    With ThisWorkbook.Worksheets("Bil")
                       .Cells(i, ii) = Container(ii - 1)
                    End With
                Next ii
            
            
    Loop
Close #1
End Sub

Bonne Fin de Journée

@+Thierry
 

thierry_44

XLDnaute Junior
Re : Import d'un fichier txt par macro

Merci c'est ok

par contre , je voudrais si c'est possible recuperer la date ex format fichier txt:08/08/2007 02:51 , en deux colonnes dans la feuille bil excel date 08/08/2007 et heure 02:51.

a+

et encore merci

thierry44
 

Bebere

XLDnaute Barbatruc
Re : Import d'un fichier txt par macro

Bonsoir aux 2 Thierry,Skoobi
en changeant le code de Thierry l'intervenant
le code dans module 2,le résultat dans feuille Bil
le fichier texte créé avec notepad est avec
à bientôt
 

Pièces jointes

  • ClasseurThierry44.zip
    28.5 KB · Affichages: 53
  • ClasseurThierry44.zip
    28.5 KB · Affichages: 46
  • ClasseurThierry44.zip
    28.5 KB · Affichages: 52

pierrejean

XLDnaute Barbatruc
Re : Import d'un fichier txt par macro

bonjour thierry 44

Salut à tous

a partir du code de l'excellentissime Thierry

Code:
Option Explicit
Sub ImportTXT()
Dim Record As String
Dim Container As Variant
Dim i As Integer, ii As Byte
Dim ThePath As String
 
ThePath = ThisWorkbook.Path & "\tempost.txt" 'A Adapter...
Open ThePath For Input As #1
    
    Do While Not EOF(1)
        i = i + 1
        Line Input #1, Record
        Container = Split(Record, Chr(9))
            
            
                For ii = 1 To 3
                
                    With ThisWorkbook.Worksheets("Bil")
                     If IsDate(Container(ii - 1)) Then
                       .Cells(i, ii) = CDate(Container(ii - 1))
                     Else
                       .Cells(i, ii) = Container(ii - 1)
                     End If
                    End With
                Next ii
            
            
    Loop
Close #1
End Sub
 

pierrejean

XLDnaute Barbatruc
Re : Import d'un fichier txt par macro

re

a tester:

Code:
Option Explicit
Sub ImportTXT()
Dim Record As String
Dim Container As Variant
Dim i As Integer, ii As Byte
Dim ThePath As String
 
ThePath = ThisWorkbook.Path & "\tempost.txt" 'A Adapter...
Open ThePath For Input As #1
 
    Do While Not EOF(1)
        i = i + 1
        Line Input #1, Record
        Container = Split(Record, Chr(9))
 
 
                For ii = 1 To UBound(Container) + 1
                    With ThisWorkbook.Worksheets("Bil")
                     If IsDate(Container(ii - 1)) Then
                       .Cells(i, ii) = CDate(Container(ii - 1))
                     Else
                       .Cells(i, ii) = Container(ii - 1)
                     End If
                    End With
                Next ii
 
 
    Loop
Close #1
End Sub
 
Dernière édition:

thierry_44

XLDnaute Junior
Re : Import d'un fichier txt par macro

Dans le dernier cas de pierrejean ,une ligne dans le fichier txt devient un enregistrement dans une cellule excel.

La premiere version de thierry fonctionne bien , par contre probleme de conversion de la date txt:jj/mm/aaaa devient mm/jj/aaaa dans excel.

a+

thierry44
 

pierrejean

XLDnaute Barbatruc
Re : Import d'un fichier txt par macro

re

suis tres surpris !!

chez moi cela fonctionne avec tempost.txt

voir fichiers joints
 

Pièces jointes

  • Classeur2.zip
    13.4 KB · Affichages: 22
  • Classeur2.zip
    13.4 KB · Affichages: 26
  • Classeur2.zip
    13.4 KB · Affichages: 23

thierry_44

XLDnaute Junior
Re : Import d'un fichier txt par macro

Bonsoir

je viens d'essayer c'est ok en effet par contre la récupération d'une ligne txt est dans excel met la date et heure dans la meme cellule.
est il possible d'avoir la date et l'heure séparée ?

Merci

thierry44:)
 

pierrejean

XLDnaute Barbatruc
Re : Import d'un fichier txt par macro

bonjour thierry 44

voila pour separer la date et l'heure

notes:
1) si dans une ligne une autre donnée est du type date ,il n'y aura qu'une heure transcrite (correspondante a la derniere donnée date)

2) les lignes incompletes seront traitées pour autant qu'il y ait au moins une donnée ,ce qui n'etait pas le cas de ma 1ere version
 

Pièces jointes

  • Tempost.zip
    10.3 KB · Affichages: 39
  • Tempost.zip
    10.3 KB · Affichages: 37
  • Tempost.zip
    10.3 KB · Affichages: 36

Discussions similaires

  • Résolu(e)
Microsoft 365 Tri et Import
Réponses
4
Affichages
160
Réponses
11
Affichages
498

Statistiques des forums

Discussions
312 106
Messages
2 085 352
Membres
102 871
dernier inscrit
Maïmanko