VBA 使用通配符打开工作簿?

VBA Open workbook with wildcard?

我在以下目录中有一个 .xlsx 文件:

G:\BUYING\Food Specials. Planning. Planning. Planner. 201717 Planner.xlsx

我可以将它指向这个目录,但目录会根据年份而改变。

所以这个目录可以变成:

G:\BUYING\Food Specials. Planning. Planning. Planner. 201818 Planner.xlsx

为了处理这个问题,我正在尝试像这样向我的路径添加通配符:

G:\BUYING\Food Specials. Planning. Planning. Planner\" & "*." & " " & Year(Date) & "\"

我的工作簿打不开。 有人可以告诉我哪里出错了吗?

子:

'Find Planner
If Len(FindDepotMemo) Then
Set wb2 = Workbooks.Open(FindDepotMemo, ReadOnly:=True, UpdateLinks:=False)
End If

函数:

Function FindDepotMemo() As String

    Dim Path As String
    Dim FindFirstFile As String

    Path = "G:\BUYING\Food Specials. Planning. Planning. Planner\" & "*." & " " & Year(Date) & "\"

    FindFirstFile = Dir$(Path & "*.xlsx")

    While (FindFirstFile <> "")

        If InStr(FindFirstFile, "Planner") > 0 Then

            FindDepotMemo = Path & FindFirstFile
            Exit Function

        End If

        FindFirstFile = Dir

    Wend

End Function

试试这个方法。代码循环遍历文件夹名称 1. 2020、1. 2019、1. 2018 和当前年份,找到最低的一个,例如 1. 2018 存在。然后,对于那一年,它会寻找最高的月份数字,放弃前一个月份,寻找下一个更高的月份。例如,如果 4. 2018 存在,那就是它使用您的原始代码在其中查找文件名的路径。

Function FindDepotMemo() As String

    Dim Pn As String                                ' Path name
    Dim Fn As String                                ' Folder name
    Dim Sp() As String                              ' Split string
    Dim Arr() As String, Tmp As String
    Dim FindFirstFile As String
    Dim i As Integer
    Dim n As Integer

    For i = 2020 To Year(Date) Step -1
        Pn = "G:\BUYING\Food Specials. Planning. Planning. Planner. " & CStr(i) & "\"
        If Len(Dir(Pn, vbDirectory)) Then Exit For
    Next i

    If i >= Year(Date) Then
        Sp = Split(Pn, "\")
        Arr = Sp
        n = UBound(Sp) - 1
        Fn = Sp(n)
        For i = 2 To 12
            Arr(n) = CStr(i) & Mid(Fn, 2)
            Pn = Join(Arr, "\")
            If Len(Dir(Pn, vbDirectory)) = 0 Then Exit For
            Sp = Arr
        Next i

        FindFirstFile = Join(Sp, "\") & "*.xlsx"
        While (FindFirstFile <> "")
            If InStr(FindFirstFile, "Planner") > 0 Then
                FindDepotMemo = Pn & FindFirstFile
                Exit Do
            End If
            FindFirstFile = Dir
        Wend
    End If
End Function

如果没有找到 1. 2017 或更高版本,函数 returns 一个空字符串。由于缺少数据,我无法测试代码。

有很多方法,但我会使用这种方法:

Function FindDepotMemo() As String

    Dim oldPath, tempPath, newPath As String
    Dim FindFirstFile As String
    Dim addInt as Integer

    ' ASSUMING YOU ALREADY HAVE THIS PATH
    ' WE WILL USE THIS AS BASE PATH
    ' AND WE WILL INCREMENT THE NUMBER AND YEAR IN IT
    oldPath = "G:\BUYING\Food Specials. Planning. Planning. Planner. 2017"

    ' EXTRACT 8. 2017 FROM oldPath
    tempPath = Mid(oldPath, InStrRev(oldPath, "\") + 1)

    ' GET THE YEAR DIFFERENCE
    addInt = Year(Date) - CInt(Split(tempPath, ".")(1))

    ' ADD THIS DIFFERENCE TO NUMBER AND YEAR
    ' AND NOW YOU HAVE CURRENT YEAR FOLDER
    newTemp = Split(tempPath, ".")(0) + addInt & ". " & Split(tempPath, ".")(1) + addInt

    FindFirstFile = Dir$(Path & "\" & "*.xlsx")

    While (FindFirstFile <> "")
        If InStr(FindFirstFile, "Test") > 0 Then
            FindDepotMemo = Path & FindFirstFile
            Exit Function
        End If
        FindFirstFile = Dir
    Wend

End Function

因为我添加了评论以帮助您了解我在做什么。