2015-05-04 42 views
0

我有一些麻煩讓我的進口工作:導入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文件有關。範圍在xlRange1xlRange2上失敗。如果我把xlSheet.Select它似乎並沒有解決它。

我很困惑這一點。此外,我無法找到如何遞歸地遍歷子目錄(例如,將所有Excel文件導入數據中)

Access 2010,帶有Excel 2010 XLSX文件。

回答

0

如上所述,您需要的範圍與 B2:F10相似,而不是解析 Range對象。

所以,你可以使用像你的範圍Address屬性:

Range:=xlRange1.Address 

我無法測試它。

0

您正試圖在文件打開時導入數據,這會給您帶來一些問題。 你想做什麼,打開excel文件,設置你的範圍,保存工作簿,導入你的範圍,然後返回並清除命名範圍,如果你願意。

如果您要導入一個工作表中存儲的兩組不同的數據,您可能需要考慮將它們拆分到兩個單獨的工作表上,以使您的生活更加輕鬆。

目前雖然,類似;

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) 

它更容易使用一個變量捕捉到你上次使用的排 如

dim lastRow as integer 
lastrow = xlSheet.Range("A1").End(xlDown) 'set the value of our last used row 
xlSheet.Range("A1:M" & lastRow & "").name = "xlRange1" 'apply a name to our range 
lastrow = xlSheet.Range("n1").End(xlDown) 'reset our last row value 
xlSheet.Range("N1:'enterlastcolumnhere'" & lastRow & "").name = "xlRange2" 

xlFile.save 'save our named ranges 
xlApp.Quit 'close off 
Set xlApp = Nothing 



'Import File Range A 

DoCmd.TransferSpreadsheet _ 
    transfertype:=acImport, _ 
    tablename:=strTable, _ 
    FileName:=strPath & "\" & strFile, _ 
    hasFieldNames:=False, _ 
    Range:="xlRange1" 'this should now work if we didn't get any errors when setting the named ranges 

'Import File Range B 
DoCmd.TransferSpreadsheet _ 
    transfertype:=acImport, _ 
    tablename:=strTable, _ 
    FileName:=strPath & strFile, _ 
    hasFieldNames:=False, _ 
    Range:="xlRange2" 

端功能

我已經寫了從內存代碼的變化,但這種應該把你放在正確的軌道上,如果你仍然陷入困境,讓我知道我可以挖掘我爲你寫的以前的例子! HTH

+0

我無法修改原始工作表,因爲它是隻讀的。我會看看我是否可以保存第一行和最後一行,然後看看它是否有效。 – Idgarad

+0

如果工作表是隻讀的,請執行另存爲,然後刪除只讀屬性,導入您的數據,然後刪除副本將是一個好方法 –

+0

找到了解決方案。必須打開它兩次。 Transfer Spreadsheet不會起作用,所以我最終打開它,找到範圍,然後使用ACE通過SQL讀取excel並對結果執行INSERT INTO。 – Idgarad