我有以下目錄中.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
這似乎是你的問題的公平答案。 http://stackoverflow.com/questions/19527415/using-a-wildcard-to-open-an-excel-workbook?rq=1 – Variatus
我不明白你爲什麼不知道你想要的* exact *文件打開?當然,你需要像'strYearOfInterest'這樣的用戶輸入,然後使用該輸入來設置一個變量,並將其傳遞到文件名的字符串中? –
@Variatus謝謝我試過這個,但我認爲它是因爲即時通訊使用通配符和數字,它不是相同的 – user7415328