感谢大家的帮助。我得到了用文件前缀读取的代码。我希望它循环并导入所有以Import *开头的excel工作簿。但是,当下面的代码循环时,它会一直询问它刚才处理的文件名,即使它已经被关闭,复制并从其当前目录中删除。我虽然对Nothing的Set语句会解决它,但事实并非如此。如何让Access删除以前的文件名并转到下一个?
Sub Count()
Dim xl As Excel.Application
Dim xlwrksht As Excel.Worksheet
Dim xlWrkBk As Excel.Workbook
Dim nIndex As Integer
Dim strMvPath As String
Dim mvPath As String
Dim strFile As String
Set rs = CurrentDb.OpenRecordset("Import_TMP", dbOpenTable)
strPath = "C:\Users\...\Documents\Blah\"
strMvPath = "C:\Users\...\Documents\Blah\Completed\"
strFile = Dir(strPath & "Import*.xls")
Do While strFile <> ""
Set xlWrkBk = Workbooks.Open(filename:=strPath & strFile)
For nIndex = 1 To xlWrkBk.Worksheets.Count
If xlWrkBk.Worksheets(nIndex).Name Like "Component*" Or xlWrkBk.Worksheets(nIndex).Name Like "Import*" Or xlWrkBk.Worksheets(nIndex).Name Like "Child*" Or xlWrkBk.Worksheets(nIndex).Name Like "R*" Then
.... data fields to import
End If
Next
xlWrkBk.Close SaveChanges:=False
Set xlWrkBk = Nothing
FileCopy strPath & strFile, strMvPath & Format(Now(), "YYYY_MM_DD_HH_MM") & "_IQS.xls"
Kill strPath & strFile
Loop
rs.Close
End Sub
我的VBA低于标准杆,而且我可能很简单。