2017-04-06 196 views
0

我有以下目錄中.xlsx文件:VBA用通配符打開工作簿?

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 
+2

這似乎是你的問題的公平答案。 http://stackoverflow.com/questions/19527415/using-a-wildcard-to-open-an-excel-workbook?rq=1 – Variatus

+2

我不明白你爲什麼不知道你想要的* exact *文件打開?當然,你需要像'strYearOfInterest'這樣的用戶輸入,然後使用該輸入來設置一個變量,並將其傳遞到文件名的字符串中? –

+0

@Variatus謝謝我試過這個,但我認爲它是因爲即時通訊使用通配符和數字,它不是相同的 – user7415328

回答

0

的方法有很多,但是,我會用這種方法:

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 

正如我添加了註釋,以幫助您瞭解我在做什麼。

0

試試這個辦法。代碼循環瀏覽文件夾名稱1. 2020,20 1.,209,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或更高,則函數返回一個空字符串。我無法測試缺少數據的代碼。

相關問題