Bonjour,
Je cherche à ouvrir et à récupérer automatiquement les données de plusieurs fichiers dont je connais partiellement les noms sur une clef USB.
Pour la première partie, j'utilise le code suivant:
J'arrive à récupérer le dernier fichier de chaque série de nom. Est-il par ailleurs possible de récupérer non pas le dernier mais l'avant dernier fichier de chaque série?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 Dim MyPath As String Dim MyFile As String Dim LatestFile As String Dim LatestDate As Date Dim LMD As Date Dim FNAME As String MyPath = "D:\test" If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\" MyFile = Dir(MyPath & "Pouet_Canard_Coin" & "*.csv", vbNormal) If Len(MyFile) = 0 Then Exit Sub End If Do While Len(MyFile) > 0 LMD = FileDateTime(MyPath & MyFile) If LMD > LatestDate Then LatestFile = MyFile LatestDate = LMD End If MyFile = Dir Loop Workbooks.OpenText MyPath & LatestFile, origin:=xlWindows, Local:=True FNAME = MyPath & LatestFile
Comme les fichiers sont sur clé usb, j'aurais voulu m'affranchir de la lettre du lecteur en adaptant la formule suivante:
J'ai tenté ce code-ci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 Dim Lecteur As Variant Dim L As Long Lecteur = Array("D", "E", "F", "H") For L = LBound(Lecteur) To UBound(Lecteur) File = Dir(Lecteur(L) & ":\fichiertest.csv") If Len(File) > 0 Then Workbooks.Open Filename:=Lecteur(L) & ":\fichiertest.csv" Exit For End If Next L
Avec l'affiche de la msgbox, je peux voir que déjà il ne prend pas la lettre du lecteur dans MyPath.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 Dim MyPath As String Dim MyFile As String Dim LatestFile As String Dim LatestDate As Date Dim LMD As Date Dim FNAME As String Dim Lecteur As Variant Dim L As Long Lecteur = Array("D", "E", "F", "H") For L = LBound(Lecteur) To UBound(Lecteur) MyPath = Dir(Lecteur(L) & ":\test") If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\" MsgBox MyPath, vbOKOnly MyFile = Dir(MyPath & "Pouet_Canard_Coin" & "*.csv", vbNormal) If Len(MyFile) = 0 Then Exit Sub End If Do While Len(MyFile) > 0 LMD = FileDateTime(MyPath & MyFile) If LMD > LatestDate Then LatestFile = MyFile LatestDate = LMD End If MyFile = Dir Loop Workbooks.OpenText MyPath & LatestFile, origin:=xlWindows, Local:=True FNAME = MyPath & LatestFile Next L
Avez-vous des idées?
Merci d'avance
Partager