您好我需要一些帮助来完成我在Access中放置的以下代码。我正在寻找修改有关mfile变量的代码的建议,以便匹配文件变量如何迭代目录中的所有Excel文件。
Public Function load_data()
'mfile is modified filename with "xl" prefix to group all imported tables
together
Dim file, mfile As Variant
file = Dir(CurrentProject.Path & "\")
mfile = "xl_" & Left(file, Len(file) - 5)
Do Until file = ""
Debug.Print mfile
If file Like "*.xlsx" Then
For Each tbl In CurrentDb.TableDefs
If mfile = tbl.Name Then
DoCmd.DeleteObject acTable, tbl.Name
Exit For
End If
Next tbl
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, mfile, CurrentProject.Path & "\" & file, True
End If
file = Dir
Loop
End Function
答案 0 :(得分:1)
我想你只想把这条线移到你的循环中?
Public Function load_data()
'mfile is modified filename with "xl" prefix to group
' all imported tables together
Dim file, mfile As Variant
file = Dir(CurrentProject.Path & "\")
Do Until file = ""
If file Like "*.xlsx" Then
mfile = "xl_" & Left(file, Len(file) - 5) '<< move inside loop
Debug.Print mfile
For Each tbl In CurrentDb.TableDefs
If mfile = tbl.Name Then
DoCmd.DeleteObject acTable, tbl.Name
Exit For
End If
Next tbl
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
mfile, CurrentProject.Path & "\" & file, True
End If
file = Dir
Loop
End Function