VBA to Browse for Excel file, clean cells and import into Access table

I'm not sure if it is possible to do the following from within Access. I usually use Visual Basic 6, so I'm not sure what I can do in VBA.

I would like to have a form that contains a control to select an Excel Spreadsheet. The spreadsheet should then be loaded into a table in Access.

I can load the spreadsheet into the table by using DoCmd.TransferSpreadsheet, but only with a hard coded value for the spreadsheet location.
If it is not possible to browse for the file, then I could use a text field to type in the location of the spreadsheet, but the first option is preferable.

A further complication is that the spreadsheet contains some carriage return characters in some of the cells, which I need to clean up so that I get all the data across to the table. (Currently all data in the cell, after the carriage return, is lost). I could do this from Excel, but unfortunately I do not have control of the spreadsheet, so I can't use macros in the spreadsheet.

I'm using Access and Excel 2002

Is this possible, and if so, how would I do it?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

get file name :->  http://www.mvps.org/access/api/api0001.htm

Now for the other bit...

You can manipulate Excel from Access just like you can in VB 6.0 (VBA isnt all that different)

My thoughts...

Get file name
Copy the file using   Filecopy source, destination  to create a "temp" copy of the excel file so we can "play" with it

Use CreatObeject("excel.application")  (or use early binding, your choice) to get an excel object (like in VB)

Manipulate your "temp" excel file to parse out the hard returns
Save temp file
Use docmd.transferSpreadsheet to import it
Delete temp file using Kill filename


What do you need help with??


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chrishorakAuthor Commented:
Thanks Dave - that is very useful

My only problem now seems to be the removal of the carriage returns/line spaces.

I am looping through all the cells in the worksheet to do the clean (using WorksheetFunction.Clean or Application.substitute) but it takes a very long time in either case.

When I'm manipulating my temp Excel file - is that being done in memory?

Any ideas how to speed it up?

You could use early binding, that should speed it up

rather than

Dim xl as object
set xl = createobject("excel.application")


Dim xl as Excel.Application
Set xl = new excel.application

To do this you'll need to add a refrence the the Excel Object Library

In vba window - Tools - Refrences and Select Microsoft Excel Object Library ?.? (pick your version)

>> When I'm manipulating my temp Excel file - is that being done in memory?

It *SHOULD* be done just like as it would in Excel, except its "hiden" from the user (unless you add a xl.visible = true)

ps.  DOnt forget to clean up


xl.quit 'close excel
set xl = nothing

Dave :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.