简体   繁体   中英

Excel Multiple worksheets to Access tables

I have received a database dump from someone in a single Excel file .xlsx that has over 50 worksheets in it. In order for me to make sense of it, I will have to upload it to a database and start pulling some information together.

I am more comfortable with Ms Access so staying with it for now.

How can I import this single file with all 50 worksheets inside into 50 tables separately in Access db?

Can someone please help as I am getting error message when using a simple External data option in Access.

Use Excel.Application Object, to get from the Excel Workbook all the names in the Worksheets Collection, and register all names of sheets in the workbook:

Dim App As Object                                            'Excel interface
Dim File As Object                                           'Your file     
Dim ws As Variant                                             'Your worksheet

Set App = CreateObject("Excel.Application")                  'This opens excel application windows
Set File = App.Workbooks.Open(fileName)                      'This opens your particular excel file (or workbook)

For each ws In File
    'Register Name in a table named tblSheetNames, Containing a field named 'ShtNm': 
    CurrentDb.Execute "INSERT INTO tblSheetNames(ShtNm) VALUES ('" & ws.Name & "')"
Next
'Close the App to allow link to the Excel file
App.Close: Set App = Nothing

Then import all these worksheets:

'Import all these worksheets:
Dim rst as DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblSheetNames")
Do While Not rst.Eof
    'Link to SpreadSheet, **make sure you add the '!' to the spread-sheet-name for the *Range* parameter**
    DoCmd.TransferSpreadsheet acLink, acSpreadsheetTypeExcel12, "Temp", fileName, rst!ShtNm & "!"
    'Dump sheet into table 'MyTable' and then Delete Link:
    CurrenDb.TableDefs.Delete("Temp")
rst.MoveNext: Loop

add the '!' to the spread-sheet-name for the Range parameter

'Import Data from All EXCEL Files in a single Folder via TransferSpreadsheet (VBA)'

Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean

' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False

' Replace C:\Documents\ with the real path to the folder that
' contains the EXCEL files
strPath = "C:\Documents\"

' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"

strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
      strPathFile = strPath & strFile
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames

' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
'       Kill strPathFile

      strFile = Dir()
Loop

The idea comes straight form here.

http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpFolderFiles

Check out the link. You will find several other really cool and utilitarian concepts there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM