macro d'importation txt et date

  • Initiateur de la discussion Initiateur de la discussion jopont
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

jopont

XLDnaute Impliqué
Bonjour

j'utilise la macro suivante pour importer plusieurs fichiers txt en même temps dans une feuille excel.

Mon problème est le suivant, lorsqu'un j'importe dans le répertoire ""E:\fevrier08" par exemple, ou il y a autant de fichiers que de jours (0102.txt,0202.txt ...) l'importation ne se fait pas dans l'ordre des jours.

D'autre part les données importées des fichiers txt sont parfois erronées, j'ai des cellules avec date qui sont parfois fausses. exemple pour le 10/02/08 08:00, la donnée importée donne 02/10/08 08:00. Il y a une inversion sur certain jour.

Je joint le fichier excel avec deux fichiers txt.

merci pour votre aide

La macro :

Sub ouvrir()
Dim Directory As String, File As String, Temp As String
Dim NumRow As Long, NumCol As Integer
Dim FF As Integer, I As Integer

Directory = "G:\février08\"
File = Dir(Directory & "*.txt")
NumRow = ActiveCell.Row
NumCol = ActiveCell.Column
With ActiveSheet
FF = FreeFile
Do While File <> ""
Open Directory & File For Input As #FF
Do While Not EOF(FF)
Line Input #FF, Temp
Table = Split(Temp, vbTab)
For I = 0 To UBound(Table)
.Cells(NumRow, NumCol + I) = Table(I)
Next
NumRow = NumRow + 1
Loop
Close #FF
File = Dir
Loop
End With
End Sub
 

Pièces jointes

Re : macro d'importation txt et date

Bonjour jopont,

Voici une solution (à tester) :

Code:
Sub import()
    Dim Directory As String, File As String, Temp As String
    Dim NumRow As Long, NumCol As Integer
    Dim FF As Integer, I As Integer
    
    Directory = ThisWorkbook.Path & "\" & "fevrier08\"   ' "E:\fevrier08\"
    File = Dir(Directory & "*.txt")
    NumRow = ActiveCell.Row
    NumCol = ActiveCell.Column
    With ActiveSheet
        FF = FreeFile
        Do While File <> ""
            Open Directory & File For Input As #FF
            Do While Not EOF(FF)
                Line Input #FF, Temp
                Table = Split(Temp, vbTab)
                For I = 0 To UBound(Table)
[COLOR="Red"]                    If IsDate(Table(I)) Then
                        .Cells(NumRow, NumCol + I) = CDate(Table(I))
                    Else[/COLOR]
                        .Cells(NumRow, NumCol + I) = Table(I)
[COLOR="red"]                    End If[/COLOR]
                Next
                NumRow = NumRow + 1
            Loop
            Close #FF
            File = Dir
        Loop
    End With
End Sub
 
Re : macro d'importation txt et date

Bonjour jopont,

Peux-tu tester ce code ?

Code:
Sub import()
    Dim Directory As String, File As String, Temp As String
    Dim NumRow As Long, NumCol As Integer
    Dim FF As Integer, I As Integer
    [COLOR="Red"]Dim LigFic As Long[/COLOR]
    
    Directory = ThisWorkbook.Path & "\" & "fevrier08\"   ' "E:\fevrier08\"
    File = Dir(Directory & "*.txt")
    NumRow = ActiveCell.Row
    NumCol = ActiveCell.Column
    With ActiveSheet
        FF = FreeFile
        [COLOR="red"]LigFic = 0[/COLOR]
        Do While File <> ""
            Open Directory & File For Input As #FF
            Do While Not EOF(FF)
                [COLOR="red"]If LigFic > 4 Then[/COLOR]
                    Line Input #FF, Temp
                    Table = Split(Temp, vbTab)
                    For I = 0 To UBound(Table)
                        If IsDate(Table(I)) Then
                            .Cells(NumRow, NumCol + I) = CDate(Table(I))
                        Else
                            .Cells(NumRow, NumCol + I) = Table(I)
                        End If
                    Next
                    NumRow = NumRow + 1
                [COLOR="red"]End If
                LigFic = LigFic + 1[/COLOR]
            Loop
            Close #FF
            File = Dir
        Loop
    End With
End Sub
 
Re : macro d'importation txt et date

Bonsoir,

Une autre solution.

Code:
Sub import()
    Dim Directory As String, File As String, Temp As String
    Dim NumRow As Long, NumCol As Integer
    Dim FF As Integer, I As Integer
    Dim LigFic As Long
    
    Directory = ThisWorkbook.Path & "\" & "fevrier08\"   ' "E:\fevrier08\"
    File = Dir(Directory & "*.txt")
    NumRow = ActiveCell.Row
    NumCol = ActiveCell.Column
    With ActiveSheet
        FF = FreeFile
        LigFic = 0
        Do While File <> ""
            Open Directory & File For Input As #FF
            Do While Not EOF(FF)
                Line Input #FF, Temp
                If LigFic > 4 Then
                    Table = Split(Temp, vbTab)
                    For I = 0 To UBound(Table)
                        If IsDate(Table(I)) Then
                            .Cells(NumRow, NumCol + I) = CDate(Table(I))
                        Else
                            .Cells(NumRow, NumCol + I) = Table(I)
                        End If
                    Next
                    NumRow = NumRow + 1
                End If
                LigFic = LigFic + 1
            Loop
            LigFic = 0
            Close #FF
            File = Dir
        Loop
    End With
End Sub
 
Re : macro d'importation txt et date

Voici une nouvelle adaptation :
Code:
Sub import()
    Dim Directory As String, File As String, Temp As String
    Dim NumRow As Long, NumCol As Integer
    Dim FF As Integer, I As Integer
    Dim LigFic As Long
    
    Directory = ThisWorkbook.Path & "\" & "fevrier08\"   ' "E:\fevrier08\"
    File = Dir(Directory & "*.txt")
    NumRow = ActiveCell.Row
    NumCol = ActiveCell.Column
    With ActiveSheet
        FF = FreeFile
        LigFic = 0
        Do While File <> ""
            Open Directory & File For Input As #FF
            Do While Not EOF(FF)
                Line Input #FF, Temp
                If LigFic > 4 Then
                    Table = Split(Temp, vbTab)
                    For I = 0 To UBound(Table)
                        If IsDate(Table(I)) Then
                            .Cells(NumRow, NumCol + I) = CDate(Table(I))
                        Else
                            .Cells(NumRow, NumCol + I) = Table(I)
                        End If
                    Next
                    NumRow = NumRow + 1
                End If
                LigFic = LigFic + 1
            Loop
            ' Supprimer la dernière ligne
[COLOR="Red"]            If NumRow > ActiveCell.Row Then
                NumRow = NumRow - 1
                .Rows(NumRow & ":" & NumRow).Delete shift:=xlUp
            End If[/COLOR]            
            LigFic = 0
            Close #FF
            File = Dir
        Loop
    End With
End Sub
 
importation texte via une combo.

Bonjour, pour faire suite à ce post, j'aimerais savoir comment je pourrais intégrer une combo qui s'ouvre à l'exécution de la macro. dans cette combo j'aimerais un champ texte qui récupère le nom du fichier txt à importer.
Ceci m'éviterais de rentrer à chaque fois dans la macro pour changer le nom du fichier à importer.

merci
 
Re : macro d'importation txt et date

Bonjour


Une piste à développer

Code:
Sub Nomdufichier()
'source:Daniel Josserand
Dim NomFichier
NomFichier = Application.GetOpenFilename
If VarType(NomFichier) = vbBoolean Then MsgBox "Action annulée" _
Else MsgBox "Fichier sélectionné : " & NomFichier
End Sub
 
Dernière édition:
Notre forum d’entraide est 100 % gratuit et le restera.
Aucune formation payante, aucun fichier à acheter, rien à vendre. Mais comme tout site, nous devons couvrir nos frais pour continuer à vous accompagner.
Soutenez-nous en souscrivant à un compte membre : c’est rapide, vous choisissez simplement votre niveau de soutien et le tour est joué.

Je soutiens la communauté et j’accède à mon compte membre

Discussions similaires

Réponses
2
Affichages
1 K
  • Question Question
Microsoft 365 Erreur de macro
Réponses
4
Affichages
690
Réponses
2
Affichages
607
Réponses
3
Affichages
854
Retour