Je tente d'écrire un script VBA qui importe tous les fichiers Excel d'un dossier dans une table dans Access 2003, en vérifiant d'abord s'ils ont importé ou non. Cette partie est bien. Le problème que je rencontre est de supprimer certaines des formules qui ne sont pas utilisées sur la feuille de calcul, ce qui provoque des difficultés lorsque Access tente d'importer la plage. lors de l'exécution du code tel quel, j'obtiens une erreur "Type défini par l'utilisateur non défini". J'utilise la liaison tardive puisque je développe pour un site qui utilise plusieurs versions d'Office et ne peut pas référencer la même bibliothèque en utilisant une liaison anticipée. Le code problème est ci-dessous:"Type défini par l'utilisateur non défini" pour la plage Excel utilisant la liaison tardive dans Access 2003
Private Sub Command2_Click()
'Declare Variables
Dim xlApp As Object
Dim xlBook As Object
Dim LSQL As String
Dim SkippedCounter As Integer
Dim ImportedCounter As Integer
Dim BUN As Long
Dim SubmitDate As Date
Dim LSQL2 As String
Dim LSQL3 As String
'Start counters for final notice
SkippedCounter = 0
ImportedCounter = 0
Dim myDir As String, fn As String
'Set directory for importing files
myDir = "U:\Five Star\Operations\restore\Surveys\My InnerView - 2010\Action plans\Action plans - input for DB\"
'Function for selecting files in folder
fn = Dir(myDir & "*.xls")
'Determine if there are files in side the folder
If fn = "" Then
MsgBox "Folder is Empty!"
Else
'Begin cycling through files in the folder
Do While fn <> ""
'Create new Excel Object
Set xlApp = CreateObject("Excel.Application")
'Make it appear on the screen while importing
xlApp.Visible = True
'Open the workbook at hand
Set xlBook = xlApp.Workbooks.Open(myDir & fn)
'Check to see if it has been imported already
If xlBook.Sheets("Action plan form").Range("A1").Value = "Imported" Then
'If it has been imported, add 1 to the counter, close the file and close the instance of Excel
SkippedCounter = SkippedCounter + 1
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Else
'Otherwise, unprotect the worksheet
xlBook.UnProtect Password:="2010"
Dim c As Range
'Unhide worksheet needed and clean it up
xlBook.Sheets("Action plan DB data").Visible = True
xlBook.Sheets("Action plan DB data").Range("B10:O10").ClearFormats
xlBook.Sheets("Action plan DB data").Range("N11:N84").ClearFormats
For Each c In xlBook.Sheets("Action plan DB data").Range("DB_import")
If c.Value = "" Or c.Value = 0 Then c.Clear
Next c
...
Le reste du code devrait fonctionner, il jsut a un problème avec la déclaration de « plage » et en boucle à travers elle. Merci de votre aide!
Sûrement Dim c As Objet? – Fionnuala
@Remou: "Comme objet" n'est pas strictement nécessaire, je crois, mais oui, c'est l'effet. – grammar31