我有一个Excel文件(file1),其宏包含以下行:
Shell "wscript " & SFilename, vbNormalFocus
其中“SFilename”是vbs文件的名称。
在vbs文件中我有一行:
appAccess.DoCmd.RunSavedImportExport ("ImportMonthlyData")
在这一行之上有几行如:
appAccess.DoCmd.OpenQuery ("Insert Detail")
在Excel file2上的vbs中有很多Excel工作正在进行(不是包含wscript宏的同一个文件。
当我通过双击文件资源管理器中的vbs来运行vbs文件时,一切都很好。
当我通过宏从Excel文件1中运行宏时,vbs在ImportExport行上失败。失败的是它无法将Excel中的工作表归档到importexport,并且错误消息中显示的工作表名称在其末尾有一个“$”。我不知道“$”是否是Access一直在做的事情,因为它在我使用文件浏览器时或者由于某些其他原因而添加“$”时起作用。但是,我的猜测是这不是问题。
我认为问题出现是因为ImportExport试图在Excel file1而不是file2中找到工作表。
你怎么看?如果这是问题,我该如何纠正呢?答案 0 :(得分:0)
那么,您是否尝试将Excel数据导入Access?为什么现在使用VBA来运行整个事情,与运行批处理脚本相比,VBA更加神秘,更直观。
以下是从一个文件夹中的所有EXCEL文件(工作表名称在所有文件中都是相同的)中的特定工作表中导入数据的通用代码。所有具有相同工作表名称的EXCEL文件工作表必须具有相同布局和格式的数据。
Dim strPathFile As String, strFile As String, strPath As String
Dim blnHasFieldNames As Boolean
Dim intWorksheets As Integer
' Replace 3 with the number of worksheets to be imported
' from each EXCEL file
Dim strWorksheets(1 To 3) As String
' Replace 3 with the number of worksheets to be imported
' from each EXCEL file (this code assumes that each worksheet
' with the same name is being imported into a separate table
' for that specific worksheet name)
Dim strTables(1 To 3) As String
' Replace generic worksheet names with the real worksheet names;
' add / delete code lines so that there is one code line for
' each worksheet that is to be imported from each workbook file
strWorksheets(1) = "GenericWorksheetName1"
strWorksheets(2) = "GenericWorksheetName2"
strWorksheets(3) = "GenericWorksheetName3"
' Replace generic table names with the real table names;
' add / delete code lines so that there is one code line for
' each worksheet that is to be imported from each workbook file
strTables(1) = "GenericTableName1"
strTables(2) = "GenericTableName2"
strTables(3) = "GenericTableName3"
' 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 3 with the number of worksheets to be imported
' from each EXCEL file
For intWorksheets = 1 To 3
strFile = Dir(strPath & "*.xls")
Do While Len(strFile) > 0
strPathFile = strPath & strFile
DoCmd.TransferSpreadsheet acImport, _
acSpreadsheetTypeExcel9, strTables(intWorksheets), _
strPathFile, blnHasFieldNames, _
strWorksheets(intWorksheets) & "$"
strFile = Dir()
Loop
Next intWorksheets
答案 1 :(得分:0)
显然,Excel正在查找file2中的工作表。我不知道为什么,但作为解决方法,我只是将所需的工作表从file1复制到file2,完成了必要的工作,然后删除了工作表。