我有大约 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 新手,所以对编辑代码有点不确定。