打开具有部分名称的文件

发布于 2025-01-15 00:17:51 字数 320 浏览 4 评论 0原文

我正在尝试打开一个包含部分名称的文件。

当前名称为“GDE 投资组合特征 12.31.2021”。

我们的想法是打开它,无论日期如何(最后 10 个字符)。我的文件夹中应该只有一个文件具有这样的部分名称。

Workbooks.Open Filename:=ThisWorkbook.Path & "\Parametric GDE Portfolio Characteristics*.xlsx"

它找不到该文件。如果我使用文件的完整名称,它就可以工作。

I am trying to open a file with partial name.

The current name is "GDE Portfolio Characteristics 12.31.2021".

The idea is to open it, no matter the date (last 10 characters). I should only have one file in the folder with such a partial name.

Workbooks.Open Filename:=ThisWorkbook.Path & "\Parametric GDE Portfolio Characteristics*.xlsx"

It does not find the file. It works if I use the entire name of the file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2025-01-22 00:17:51

无法在 Open 语句中使用通配符。但是,您可以使用
Dir - 命令获取真实文件名,因为它允许通配符:

Dim fileName As String
fileName = Dir(ThisWorkbook.Path & "\Parametric GDE Portfolio Characteristics*.xlsx")
If fileName <> "" Then
    Workbooks.Open Filename:=ThisWorkbook.Path & "\" & fileName
End If

There is no way to use a wildcard in the Open-statement. However, you can use the
Dir-command to get the real file name as it allows wildcards:

Dim fileName As String
fileName = Dir(ThisWorkbook.Path & "\Parametric GDE Portfolio Characteristics*.xlsx")
If fileName <> "" Then
    Workbooks.Open Filename:=ThisWorkbook.Path & "\" & fileName
End If
帅的被狗咬 2025-01-22 00:17:51

这是一种更通用的方法:

Sub OpenFiles()
    Dim Files As Collection
    Set Files = ListFiles(ThisWorkbook.Path, "Parametric GDE Portfolio Characteristics*.xlsx")

    Dim Filename As Variant
    
    For Each Filename In Files
        Workbooks.Open Filename:=Filename
    Next
End Sub

Function ListFiles(FolderName As String, SearchString As String) As Collection
    Set ListFiles = New Collection
    
    Dim Filename As String
    Filename = Dir(FolderName & "\" & SearchString)
    
    If Len(Filename) = 0 Then Exit Function
    
    Do While Filename <> ""
        ListFiles.Add Filename
        Filename = Dir()
    Loop
End Function

Here is a more generic approach:

Sub OpenFiles()
    Dim Files As Collection
    Set Files = ListFiles(ThisWorkbook.Path, "Parametric GDE Portfolio Characteristics*.xlsx")

    Dim Filename As Variant
    
    For Each Filename In Files
        Workbooks.Open Filename:=Filename
    Next
End Sub

Function ListFiles(FolderName As String, SearchString As String) As Collection
    Set ListFiles = New Collection
    
    Dim Filename As String
    Filename = Dir(FolderName & "\" & SearchString)
    
    If Len(Filename) = 0 Then Exit Function
    
    Do While Filename <> ""
        ListFiles.Add Filename
        Filename = Dir()
    Loop
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文