我会把所有的Excel文件放在一个公共文件夹中,并使用VB导航到该文件夹并循环遍历所有文件以导入或链接它们。代码可以从运行时间,文件日期或文件名称中添加时间戳 - 无论您想保留哪些信息。作为帮助开始的一个示例,此功能可导航到文件夹,获取文件列表,检查文件名称以查找条件,然后将选定文件导入到表格中。它还会删除在该进程中创建的任何导入错误文件,因为我知道每个表中都有一行不会导入。
Function Import_Match_Report()
'On Error Resume Next
'Get folder & file list
Dim Source_folder As String
Source_folder = Get_Folder()
Dim FSO As New FileSystemObject
Set flist = FSO.GetFolder(Source_folder).Files
M = InputBox("update period (01-12)")
'delete prior data
CurrentDb().Execute ("delete * from [Matching Report] where period=" & M)
'Process each file
SQL = "INSERT INTO [Matching Report] ([period],[account], [reporting unit],[reporting unit title],[amount],[offset unit],[offset unit title],[offset],[variance]) "
For Each file In flist
If (Len(Dir(Source_folder & "\" & file.Name)) = 0) Then GoTo NextFile
If file.Size = 0 Then GoTo NextFile
Source = file.Name
period = Mid(Source, 7, 2)
If period <> M Then GoTo NextFile
account = Mid(Source, InStr(Source, ".") - 9, 9)
CurrentDb().Execute ("delete * from [matching report temp]")
DoCmd.TransferText acImportDelim, "match_spec", "matching report temp", Source_folder & "\" & file.Name
SQL2 = SQL & " select " & period & ",'" & account & "', F1, F2, F3, F4, F5, F6, F7 from [matching report temp]"
CurrentDb.Execute SQL2
table = Left(Source, InStr(Source, ".") - 1)
If table_exist(table & "_ImportErrors") Then DoCmd.DeleteObject acTable, table & "_ImportErrors"
NextFile: Next file
MsgBox ("data import completed")
End Function
Public Function Get_Folder()
'Create a FileDialog object as a Folder Picker dialog box.
Const msoFileDialogFolderPicker = 4
Const msoFileDialogFilePicker = 3
Const msoFileDialogViewDetails = 2
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
fd.AllowMultiSelect = False
fd.ButtonName = "Select"
fd.InitialView = msoFileDialogViewDetails
fd.Title = "Select Folder"
fd.InitialFileName = "MyDocuments\"
fd.Filters.Clear
'Show the dialog box and get the file name
If fd.Show = -1 Then
Get_Folder = fd.SelectedItems(1)
Else
Get_Folder = "MyDocuments\"
End If
Set fd = Nothing
End Function