【问题标题】:Import Excel files into Access whilst Looping循环时将 Excel 文件导入 Access
【发布时间】:2013-11-06 13:17:24
【问题描述】:

我很乐意使用 VBA 将 Excel 工作表导入访问,并循环浏览给定文件夹以恢复其中的所有内容。但是,我想遍历一个文件夹并只导入文件的选择。有人可以帮忙吗?每个文件称为REPORT1 等并运行到REPORT67。我只想选择1-47

下面的代码可以正常工作,但这只是从指定位置复制所有内容。

Sub Sample2()
Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\"
Dim strFile As String
Dim i As Long

strFile = Dir(cstrFolder & "*.xls")
If Len(strFile) = 0 Then
    MsgBox "No Files Found"
Else
    Do While Len(strFile) > 0
        Debug.Print cstrFolder & strFile

        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        strFile, cstrFolder & strFile, True

        i = i + 1
        strFile = Dir()
    Loop
    MsgBox i & " Files are imported"
End If
End Sub

【问题讨论】:

  • 要求代码的问题必须表明对所解决问题的最低理解。包括尝试的解决方案、它们为什么不起作用以及预期的结果。另见:Stack Overflow question checklist
  • 是的,我已经把我的原始代码放在上面了!!!!我现在需要帮助来调整它。无论如何感谢您的帮助。

标签: vba ms-access


【解决方案1】:

我刚刚将您的代码“Do While Len(strFile) > 0”替换为“Do While Int(Mid(strFile, 7))

Sub Sample2()
Const cstrFolder As String = "F:\TCB_HR_KPI\Data View\"
Dim strFile As String
Dim i As Long

strFile = Dir(cstrFolder & "*.xls")
If Len(strFile) = 0 Then
    MsgBox "No Files Found"
Else
    Do While Int(Mid(strFile, 7)) < 48
        Debug.Print cstrFolder & strFile

        DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        strFile, cstrFolder & strFile, True

        i = i + 1
        strFile = Dir()
    Loop
    MsgBox i & " Files are imported"
End If
End Sub

【讨论】:

    【解决方案2】:

    我会这样做: 目录$() DoWhile Val(Mid(filename, 7, 2))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 2019-07-16
      • 2019-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多