[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

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?
  • 3
1 Solution
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??

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 :-)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now