Home » excel » vba – EXCEL import to Access

vba – EXCEL import to Access

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am having some troubles with a VBA code that should import excel data to my access database. When i run the code, i get a RunTime error “Runtime error 438 Object doesn’t support this property or method “. From my readings in other forums; I understand that the problem might be a library problem (referencing a library while using another one) I’m somewhat new to VBA. I don’t know if it’s related, but before i had this error i had an “erreur 429 activeX component can’t create object” that i solved.

here is the portion of the code that i think is responsible.

      On Error GoTo erreur

'déclaration des variables
Dim app As Object
Dim wkb As Object
Dim wks As Object

'initialisation des variables
Set app = CreateObject("Excel.Application")
Set wkb = app.Workbooks.Add
Set wks = wkb.Worksheets(1)
wkb = app.Workbooks.Open("C:\Users\souleimane\Desktop\Internship\donnee_excel\table_de_corresp.xls")
wks = wkb.Worksheets("sheet1")

I tried to run the compile option on the debug menu, but nothing poped up.
Any help is appreciated, and if you think this is not the portion of the code responsible then i can post the remaining.

How to&Answers:

If you are assigning an object type to a variable then you need to use Set so the last two lines should be:

Set wkb = app.Workbooks.Open("C:\Users\souleimane\Desktop\Internship\donnee_excel\table_de_corresp.xls")
Set wks = wkb.Worksheets("sheet1")

Also, you might be able to leave out these lines:

Set wkb = app.Workbooks.Add
Set wks = wkb.Worksheets(1)

depending on whether you use the added workbook later in your code. You can definitely leave out the Set wks = wkb.Worksheets(1) though because that line has no side-effects and you reassign wks two lines later