bonjour a tous,
je débute en vba et je me trouve face a un probleme.
Ce que je cherche a faire est assez simple, en fait je liste tous les fichier excel présent dans mon repertoire pour pouvoir ensuite ajouter une ligne de données dans chacun d'eux (suite a un bug constaté lors des importation de feuilles excel dans une base de donnée access). Or a chaque éxecution de mon code j'obtiens le message d'erreur : "objet requis".
Pour plus d'information je vous met a disposition mon code.
J'espere que vous pouurez m'aider à trouver une solution merci d'avance
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 Sub Commande3_Click() On Error GoTo Err_Commande3_Click Dim xapp As Excel.Application Dim xlsheet As Excel.Worksheet Dim xlbook As Excel.Workbook Dim f As String Set xapp = CreateObject("Excel.Application") f = "aaaaa" With Application.FileSearch .LookIn = "C:\Mes documents\Tracabilité\BAG" .filename = ".xls" 'recherche du nombre de fichiers a importer If .Execute > 0 Then MsgBox "Il y a " & .FoundFiles.Count & _ " fichier(s) trouvé(s)." 'insertion de la ligne indiquant a Access le type de données For i = 1 To .FoundFiles.Count MsgBox .FoundFiles(i) Set xlbook = xapp.Workbooks.Open(.FoundFiles(i)) xlsheet.Range("A2:G2").EntireRow.Insert shift:=xlShiftDown xlsheet.Cells("A2").Value = f xlsheet.Cells("B2").Value = f xlsheet.Cells("C2").Value = f xlsheet.Cells("D2").Value = f xlsheet.Cells("E2").Value = f xlsheet.Cells("F2").Value = f xlbook.Save xlsheet = Nothing xlbook = Nothing Next i 'importation des fichiers trouvés For i = 1 To .FoundFiles.Count fichier = .FoundFiles(i) DoCmd.TransferSpreadsheet transfertype:=acImport, _ tablename:="BAG", _ Hasfieldnames:=True, _ filename:=.FoundFiles(i), _ Range:=("A:G") Next i Else MsgBox "Aucun fichier trouvé." End If End With MsgBox "Importation terminée" Exit_Commande3_Click: Exit Sub Err_Commande3_Click: MsgBox Err.Description MsgBox "Le fichier " & fichier & " est peut etre endommagé." Resume Exit_Commande3_Click End Sub
Partager