【发布时间】:2013-11-04 17:44:45
【问题描述】:
我有大约 200 个 Excel 文件,我想将它们导入到单个 Access 数据库中,并且每个文件都有一个表。每个 Excel 文件都有多个工作表,但我要导入的工作表是一致的。
我为此找到了一些代码,请参阅:http://www.accessmvp.com/KDSnell/EXCEL_Import.htm#ImpBrsFldFiles, http://social.msdn.microsoft.com/Forums/en-US/dfea25ab-cd49-495c-8096-e3a7a1484f65/importing-multiple-excel-files-with-different-file-name-into-access-using-vba
这是我尝试过的一段代码:
Option Compare Database
Sub ImportFromExcel()
End Sub
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = True
strBrowseMsg = "C:\Users\fratep\Desktop\Long-term EWM Study Data Files\"
strPath = BrowseFolder(strBrowseMsg)
If strPath = "" Then
MsgBox "No folder was selected.", vbOK, "No Selection"
Exit Sub
End If
' 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
Sub ImportMultiExcels()
End Sub
来自上面的第一个链接,但我似乎无法让他们做我正在寻找的事情。谁能帮帮我?
我是 VBA 新手,所以对编辑代码有点不确定。
【问题讨论】:
-
具体是什么问题?您可以将单个工作表导入 Access 但不能将其扩展到 200 个工作表吗?还是什么?
-
嗨,Hans,是的,据我所知,导入向导一次只能处理一张。就像我说的那样,我找到了执行该功能的 VBA 脚本,但由于某种原因,我无法让它们工作。
-
好的,然后向我们展示您的 VBA,它成功导入了单个工作表,并描述了您在尝试将其扩展到 200 个工作表时遇到的问题。
-
另外,我应该补充一点,我对 VBA 还是很陌生,所以我在阅读和根据我的情况定制它时遇到了一些麻烦。我确实更改了文件中的路径名,所以不是这样。
-
抱歉,没有使用 VBA 导入单张工作表,我使用的是导入向导。这是我的问题吗?
标签: excel ms-access ms-access-2007 vba