【问题标题】:Looping through files in directory with Dir function, just hits same file over and over [duplicate]使用 Dir 函数遍历目录中的文件,只是一遍又一遍地点击同一个文件[重复]
【发布时间】:2015-12-23 20:31:20
【问题描述】:

我有一个电子表格,可以查看给定目录中的其他几个电子表格,以从中提取一些统计信息。到目前为止,它到达了第一个文件并一遍又一遍地点击它。

我的代码是这样的:

Public Sub GetResults()
    Const strDirectory As String = "SomeNetworkLocation"
    Dim strFileName As String

    strFileName = Dir(strDirectory & "*.xl??")

    Do While strFileName <> ""

        If strFileName Like "SomeMatchingScheme_##_??"
            Workbooks.Open strDirectory & strFileName

            'Do stuff with the workbook

            Workbooks(strFileName).Close
        End If
    Loop
End Sub

有什么建议吗?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    你需要再打电话

    strFileName = Dir  ' with no parameters
    

    循环之前。此调用将 strFileName 迭代到下一个文件。没有它,它将坚持第一个找到的文件。

    【讨论】:

    • 成功了,谢谢。
    猜你喜欢
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多