簡體   English   中英

VBA用通配符打開工作簿?

[英]VBA Open workbook with wildcard?

我在以下目錄中有一個.xlsx文件:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\8. 2017\2017 Planner.xlsx

我可以僅將其指向此目錄,但是該目錄將根據其年份而更改。

因此,該目錄可能變為:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. Planner\9. 2018\2018 Planner.xlsx

為了解決這個問題,我試圖像這樣將通配符添加到我的路徑中:

G:\BUYING\Food Specials\2. Planning\1. Planning\1. 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\2. Planning\1. Planning\1. 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\2. Planning\1. Planning\1. Planner\1. " & 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或更高版本,則該函數返回空字符串。 我無法測試代碼是否缺少數據。

有很多方法,但是,我將使用這種方法:

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\2. Planning\1. Planning\1. Planner\8. 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

當我添加評論以幫助您了解我在做什么時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM