我每天收到的销售文件,我想自动导入访问。它们目前保存到具有一致命名约定的特定文件夹中。我不会每天检查这些文件,并希望使导入过程成为一个按钮过程。我不需要文件夹中的其他文件,所以我不能只导入整个文件。
文件命名约定: DAILY.SALES.20160611
(的20160611是年 - 2016年,月日 - 6月和11天)
帮助需要:
我可以导入所有文件,但我无法弄清楚如何只指定那些以“Daily.Sales”开头的文件。下面是我可以导入所有内容而不指定的代码。我的假设是,它与路径或strFile有关,但我尝试过的任何变体都没有奏效。
这将是很好,如果代码实际上可以,检查文件是否已经被上传之前上传的,但是如果我在每次使用后删除表,并重新上传的一切,这将更容易。
Dim strFile As String 'Filename
Dim strFileList() As String 'File Array
Dim intFile As Integer 'File Number
Dim filename As String
Dim path As String
DoCmd.SetWarnings False
path = "C:\Desktop\Test\"
Dim objXL As Object
Dim wb As Object
Set objXL = CreateObject("Excel.Application")
strFile = Dir(path & "*.xls")
While strFile <> ""
Set wb = objXL.Workbooks.Open(path & strFile)
If wb.Sheets(1).Range("A1") <> "No Data" And wb.Sheets(1).Range("A1") <> "" Then
'add files to the list
intFile = intFile + 1
ReDim Preserve strFileList(1 To intFile)
strFileList(intFile) = strFile
End If
strFile = Dir()
Debug.Print strFileList(intFile)
wb.Close False
Set wb = Nothing
Wend
'see if any files were found
If intFile = 0 Then
MsgBox "No files found"
Exit Sub
End If
'cycle through the list of files
For intFile = 1 To UBound(strFileList)
filename = path & strFileList(intFile)
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel8, "Stage", filename, False
Call Format_Staging_Table
Call Copy_from_Stage_to_Master
Call Clear_Staging_Table
Next intFile
DoCmd.SetWarnings True
可以忽略呼叫件,它们一旦我得到它在对数据进行格式化......
感谢任何帮助或建议,任何人可以提供!
你要吗? 'strFile = DIR(路径与“Daily.Sales的* .xls”)' – HansUp
“没有,我已经试过了工作的变化” - 很难理解你试过什么 - 几乎任何DOS文件seacrh规范将工作 - @HansUp的建议是非常简单和作品 - 一样''和' “Daily.Sales ???????? XLS。”' “Daily.Sales * XLS。” - 然后你说“T公司将如果代码实际上可以检查文件是否已在上传前上传,那么该代码很好。“ - 您正在使用MS-Access - 我假设您使用的数据库不仅仅是传输spreadhseets?它可以通过简单的日志文件表和更新记录集以及一个查询来检查它是否在表中。 – dbmitch