【问题标题】:Excel VBA - Check if file exists using Wildcard and open the fileExcel VBA - 使用通配符检查文件是否存在并打开文件
【发布时间】:2018-06-25 13:59:06
【问题描述】:

我想检查我计算机上的文件夹中是否存在文件。我有下面,可以查看是否存在特定文件:

Function FileExists(sFile As String)
    sPath = "M:\User\" & sFile & ".xlsm"
    FileExists = Dir(sPath) <> ""
End Function

但是,我的文件命名为:Filename - Version xx.xlsm 并定期更新。请注意,文件夹中只有 一个 文件,但文件名可能会有所不同。

如何使用通配符在文件夹中搜索:

Filename - Version % % 然后,如果找到任何文件,然后打开文件?

【问题讨论】:

  • &amp; sFile &amp; "*" &amp; ".xlsm"?或&amp; sFile &amp; "*.xlsm"

标签: excel vba


【解决方案1】:

一种选择是在FileExists 函数中打开文件。但是,我不建议这样做。该函数应该完全按照名称的含义执行,仅此而已。

另一种选择是稍微重构你的代码:

Private Sub OpenFile()
   Dim FileName As String

   FileName = GetFile("Filename - Version*")

   If FileName <> "" Then
      'open FileName as needed
   End If
End Sub

Private Function GetFile(sFile As String) As String
    sPath = "M:\User\" & sFile & ".xlsm"
    GetFile = Dir(sPath)
End Function

【讨论】:

    猜你喜欢
    • 2015-08-02
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 2015-11-28
    • 2019-03-30
    相关资源
    最近更新 更多