XL 2016 Importer ligne de date à date dans CSV

reve24

XLDnaute Occasionnel
Bonjour
en partant de la jolie macro de PARITEC , peux t on importer que les lignes comprises entre telle date à telle
date
Car dans un journal de donnée il y a toute l année
seule une periode variable selectionnable m interraisserait.
J e choisie cette periode
avant importation
 

Pièces jointes

  • Test.zip
    28.4 KB · Affichages: 33

job75

XLDnaute Barbatruc
Bonsoir reve24, Papou,

Pourquoi des MP ? Les solutions intéressent tout le monde :
Code:
Sub ImporterCSV()
Dim s, mini&, maxi&, t, ncol%, n&, i&, j%
s = Split(InputBox("Entrez les 2 dates jj/mm/aaaa jj/mm/aaaa :"))
If UBound(s) < 1 Then Exit Sub
If Not IsDate(s(0)) Or Not IsDate(s(1)) Then Exit Sub
mini = IIf(CDate(s(0)) < CDate(s(1)), CDate(s(0)), CDate(s(1)))
maxi = IIf(CDate(s(0)) > CDate(s(1)), CDate(s(0)), CDate(s(1)))
Application.ScreenUpdating = False
On Error Resume Next
'---analyse du fichier csv---
Workbooks.OpenText ThisWorkbook.Path & "\CSV1.csv", Local:=True 'chemin et nom à adapter
If ActiveWorkbook.Name = ThisWorkbook.Name Then MsgBox "fichier introuvable...": Exit Sub
t = ActiveWorkbook.Sheets(1).[A1].CurrentRegion
If IsArray(t) Then
    ncol = UBound(t, 2)
    n = 1
    For i = 2 To UBound(t)
        If IsDate(t(i, 1)) Then
            If t(i, 1) >= mini And t(i, 1) <= maxi Then
                n = n + 1
                For j = 1 To ncol
                    t(n, j) = t(i, j)
                Next
            End If
        End If
    Next
End If
ActiveWorkbook.Close
'---restitution---
With Feuil1 'CodeName
    If .FilterMode Then .ShowAllData 'si la feuille est filtrée
    .Cells.ClearContents 'RAZ
    .[A1].Resize(n, ncol) = t
    .Columns(2).NumberFormat = "hh:mm:ss" 'format heure en colonne B
    .Columns.AutoFit 'ajustement largeur
    With .UsedRange: End With 'actualise les barres de défilement
End With
End Sub
A+
 

reve24

XLDnaute Occasionnel
Bonjour Job75
C'est parfait , tu as créée une autre macro
Workbooks.OpenText ThisWorkbook.Path & "\CSV1.csv" le traitement va chercher plusieurs fichiers dans son dossier \*.csv
j'ai corrigé ok et et sur la macro de PARITEC en 1 ère colonne , il y avait le nom du fichier cvs ceci permettait l'analyse .
Peut on faire de meme
Merci pour la réactivité et ce joli travail
 

job75

XLDnaute Barbatruc
Bonjour reve24, Papou, le forum,

Pour traiter les fichiers CSV d'un même dossier :
Code:
Sub ImportCSV()
Dim s, mini&, maxi&, lig&, chemin$, fichier$, t, n&, ncol%, i&, j%
s = Split(InputBox("Entrez les 2 dates jj/mm/aaaa jj/mm/aaaa :"))
If UBound(s) < 1 Then Exit Sub
If Not IsDate(s(0)) Or Not IsDate(s(1)) Then Exit Sub
mini = IIf(CDate(s(0)) < CDate(s(1)), CDate(s(0)), CDate(s(1)))
maxi = IIf(CDate(s(0)) > CDate(s(1)), CDate(s(0)), CDate(s(1)))
lig = 2 '1ère ligne à remplir
Application.ScreenUpdating = False
With Feuil1 'CodeName
    If .FilterMode Then .ShowAllData 'si la feuille est filtrée
    .Rows("2:" & .Rows.Count).ClearContents 'RAZ
    chemin = ThisWorkbook.Path & "\" 'chemin à adapter
    fichier = Dir(chemin & "*.csv") '1er fichier du dossier
    While fichier <> ""
        '---analyse du fichier csv---
        Workbooks.OpenText chemin & fichier, Local:=True
        t = ActiveWorkbook.Sheets(1).[A1].CurrentRegion
        n = 0
        If IsArray(t) Then
            ncol = UBound(t, 2)
            For i = 2 To UBound(t)
                If IsDate(t(i, 1)) Then
                    If t(i, 1) >= mini And t(i, 1) <= maxi Then
                        n = n + 1
                        For j = 1 To ncol
                            t(n, j) = t(i, j)
                        Next
                    End If
                End If
            Next
        End If
        ActiveWorkbook.Close
        '---restitution---
        If n Then
            .Cells(lig, 1).Resize(n, ncol) = t
            .Cells(lig, ncol + 1).Resize(n) = "Fichier " & fichier
            lig = lig + n
        End If
        fichier = Dir 'fichier suivant
    Wend
    .Columns.AutoFit 'ajustement largeur
    With .UsedRange: End With 'actualise les barres de défilement
End With
End Sub
Fichiers joints.

Bonne journée.
 

Pièces jointes

  • Import(1).zip
    56.5 KB · Affichages: 15
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
312 492
Messages
2 088 931
Membres
103 984
dernier inscrit
maliko67