我有一些麻煩讓我的進口工作:導入Excel中使用自定義動態範圍的訪問
Function importMetrics()
Dim strFile As String
Dim strPath As String
Dim strWorksheet As String
Dim strTable As String
'Excel variables
Dim xlApp As Excel.Application
Dim xlFile As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim xlRange1 As Excel.Range
Dim xlRange2 As Excel.Range
Dim r1, r2 As String
Dim r#, c#
Dim clVal As String
'Set File Path
strPath = CurrentProject.Path & "\Data\2015\01 - January"
'check if directory exists
If Len(Dir(strPath, vbDirectory)) = 0 Then
MsgBox ("File Doesn't exist")
End
End If
'Import all Files
strFile = Dir(strPath & "\*.xl*")
'ID which Sheet to Import
strWorksheet = "EP & NBO DATA"
'ID Table to Import To
strTable = "tbl_MIPData"
'start loop
Do While strFile <> ""
'Open File
'Get the info from Excel:
Set xlApp = New Excel.Application
Set xlFile = xlApp.Workbooks.Open(strPath & "\" & strFile, False, True)
Set xlSheet = xlFile.Sheets(strWorksheet)
Set xlRange1 = xlSheet.Range("A1" & xlSheet.Range("A1").End(xlDown).End(xlToRight)).Select
Set xlRange2 = xlSheet.Range("N1" & xlSheet.Range("N1").End(xlDown).End(xlToRight)).Select
'Import File Range A
DoCmd.TransferSpreadsheet _
transfertype:=acImport, _
tablename:=strTable, _
FileName:=strPath & "\" & strFile, _
hasFieldNames:=False, _
Range:=xlRange1
'Import File Range B
DoCmd.TransferSpreadsheet _
transfertype:=acImport, _
tablename:=strTable, _
FileName:=strPath & strFile, _
hasFieldNames:=False, _
Range:=strWorksheet & "!" & xlRange2
'Close Excel File
xlApp.Quit
Set xlApp = Nothing
Loop
End Function
這是我到目前爲止已經編寫的代碼。我認爲這與我如何打開Excel文件有關。範圍在xlRange1
和xlRange2
上失敗。如果我把xlSheet.Select
它似乎並沒有解決它。
我很困惑這一點。此外,我無法找到如何遞歸地遍歷子目錄(例如,將所有Excel文件導入數據中)
Access 2010,帶有Excel 2010 XLSX文件。
我無法修改原始工作表,因爲它是隻讀的。我會看看我是否可以保存第一行和最後一行,然後看看它是否有效。 – Idgarad
如果工作表是隻讀的,請執行另存爲,然後刪除只讀屬性,導入您的數據,然後刪除副本將是一個好方法 –
找到了解決方案。必須打開它兩次。 Transfer Spreadsheet不會起作用,所以我最終打開它,找到範圍,然後使用ACE通過SQL讀取excel並對結果執行INSERT INTO。 – Idgarad