problème import de plusieurs fichiers csv

jopont

XLDnaute Impliqué
Bonjour,

J'essaye d'importer en automatique plusieurs fichiers .csv avec le code ci-dessous.
-La macro Lire fonctionne lorsque que je l’exécuté avec un fichier.
Par contre lorsque je l'exécute depuis la macro Tst, pour traiter plusieurs fichiers CSV, j'ai une erreur sur le open Fichier for input : erreur chemin fichier

-Deuxième problème, lorsque j'importe les champs de date sont parfois inversés.

Comment résoudre ces problème. Merci
Code:
Sub Tst()
Dim Fichier As Variant
    Chemin = ThisWorkbook.Path & "\"
    Fichier = Dir(Chemin & "*.csv")
    Do While Fichier <> False
        Lire Fichier
    Fichier = Dir()
    Loop
End Sub

Code:
Sub Lire(ByVal Fichier As String)
Dim Chaine As String
Dim Ar() As String
Dim i As Long
Dim iRow As Long, iCol As Long
Dim NumFichier As Integer
Dim Separateur  As String * 1
 
    '  Séparateur Point Virgule
    Separateur = ";"
     
    Cells.Clear
    Application.ScreenUpdating = False
    NumFichier = FreeFile
    iRow = 1
      Open Fichier For Input As #NumFichier
        Do While Not EOF(NumFichier)
            iCol = 1
            Line Input #NumFichier, Chaine
            Ar = Split(Chaine, Separateur)
            For i = LBound(Ar) To UBound(Ar)
                Ar(i) = Replace(Ar(i), "M-", "")
                Cells(iRow, iCol) = Ar(i)
                iCol = iCol + 1
            Next
             
           'Select Case Cells(iRow, 1)
                'Case Is = Cells(iRow, 2): Cells(iRow, 3) = "Ok"
                'Case Else: Cells(iRow, 3) = "Bad"
            'End Select
             
            iRow = iRow + 1
        Loop
    Close #NumFichier
     
    Application.ScreenUpdating = True
End Sub
 

Pierrot93

XLDnaute Barbatruc
Re : problème import de plusieurs fichiers csv

Re,

un exemple fait avec l'enregistreur de macro...

Code:
Option Explicit
Sub Macro2()
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\MESDOCS\EXCEL\nomfichier.csv", _
        Destination:=Range("$A$1"))
        .Name = "History"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 5)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
.Delete
    End With
End Sub


Sous 2010 onglet "données" => groupe "données externes" => bouton "a partir du texte"
 
Dernière édition:

jopont

XLDnaute Impliqué
Re : problème import de plusieurs fichiers csv

Dans la Sub Lire où est ce que je peux intégrer le fait de remplir à partir de la première ligne non vide ?
Où je pourrais dire également de copier sur la feuille 1 ?
Et enfin comment je résous mon problème de date ?
Je ne trouve pas la solution
 

jopont

XLDnaute Impliqué
Re : problème import de plusieurs fichiers csv

J'ai essayé avec le code ci-dessous , mais j'ai une erreur le nom du fichier n'existe pas. erreur au niveau Refresh BAckGround)
Code:
Sub Macro1()
Dim Chemin As String
Dim Fichier As String

Chemin = ThisWorkbook.Path & "\"
Fichier = Dir(Chemin & "01.csv")
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;Fichier", Destination:=Range( _
        "$A$2"))
        .Name = "01"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 4, 4)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Range("A2:G3").Select
    ActiveWorkbook.Save
    ActiveWorkbook.Save
End Sub
 

Pierrot93

XLDnaute Barbatruc
Re : problème import de plusieurs fichiers csv

Re,

tu as oublié le chemin :
Code:
Chemin = ThisWorkbook.Path & "\"
Fichier = Dir(Chemin & "01.csv")
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;Chemin & Fichier", Destination:=Range( _
        "$A$2"))
 

jopont

XLDnaute Impliqué
Re : problème import de plusieurs fichiers csv

Bonsoir,

J'ai essayé avec ta solution de QueryTables.
ça ne fonctionne plus quand je passe la variable Fichier dans la connection.
Comment faire ?
Merci
Code:
Sub Macro1()

Dim Fichier As String
ChDir ThisWorkbook.Path
Fichier = Application.GetOpenFilename("Fichier CSV (*.csv), *.csv")
    
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;Fichier", Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1))
        .Name = "01_9"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 2
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 4, 4)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    
End Sub
 

Pierrot93

XLDnaute Barbatruc
Re : problème import de plusieurs fichiers csv

Bonjour,

comme précisé post #28, il faut préciser le chemin complet... pas sur que "chdir" suffise si lecteur réseau... vérifie bien le répertoire courant, mais perso préconiserais plutôt le code que je t'ai proposé...

j'ai modifié également le post #19 en rajoutant un ".Delete" une fois les données importées...
 
Dernière édition:

Discussions similaires

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
312 756
Messages
2 091 735
Membres
105 060
dernier inscrit
DEDJAN Gaston