2016-05-25 9 views
0

50個以上のワークシートを持つ1つのExcelファイル.xlsx内の誰かからデータベースダンプを受信しました。私がそれを理解するために、私はそれをデータベースにアップロードし、いくつかの情報を一緒に取り出す必要があります。Excel複数のワークシートをアクセスするテーブル

私はMsアクセスにもっと慣れていますので、今はそのままにしておいてください。

Accessデータベース内の50個のワークシートすべてを50個のテーブルにまとめてインポートするにはどうすればよいですか?

Accessでシンプルな外部データオプションを使用しているときにエラーメッセージが表示されるので、助けてもらえますか?

+1

どのようなエラーメッセージが表示されますか? – Karlomanio

答えて

0

使用Excel.Applicationオブジェクト、エクセルWorkbookWorksheetsコレクション内のすべての名前から取得し、ワークブック内のシートのすべての名前を登録するには:

'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 

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 

はその後、すべてのこれらのワークシートをインポートします

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

+0

私の限られた知識と愚かな質問に申し訳ありません。これらのマクロを同じExcelファイルまたはAccessデータベースに挿入する場所はどこですか? – user2430036

+0

アクセス中。また、コード内の 'fileName'の値を指定する必要があります。 – marlan

+0

2番目のマクロの場合 "入力した式が引数のデータ型が間違っています。使用していますSub ImportData() DAO.Recordsetとして最初に設定します Set rst = CurrentDb.OpenRecordset tblSheetNames ") Do not Not first .EOF 'SpreadSheetにリンクすると、'! ' * Range *パラメータのスプレッドシート名** DoCmd.TransferSpreadsheet acLink、acSpreadsheetTypeExcel12、 "Temp"、 "C:\ File.xlsx"、rst!ShtNm& "!" 'シートをテーブルにダンプする' MyTable 'をクリックしてリンクを削除する: CurrenDb.TableDefs.Delete( "Temp") rst.MoveNext:ループ End Sub – user2430036

0

'Transを使用して1つのフォルダ内のすべてのEXCELファイルからデータをインポートする(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 

アイデアはストレートフォームになります。

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

リンクをご覧ください。あなたはそこにいくつかの他の本当にクールで実用的な概念を見つけるでしょう。

+0

ファインコードとリンクは@ ryguy7272を持ってきました。リクエストは1つのファイルからインポートワークシートのコードであったことに注意してください。 – marlan

関連する問題