標題:
[發問]
路徑與檔名
[打印本頁]
作者:
PJChen
時間:
2019-5-1 23:12
標題:
路徑與檔名
大大好,
請問:
1. 以下路徑的
➤日班理貨換算表
,在VBA中路徑把➤變為?而無法開啟檔案,要如何修改程式碼 or 語法?
2. 在Workbooks("OK.xlsx")程式中,其檔案名稱是OK- 5月.xlsx
但檔案因為是共用的,會有人去修改檔名,VBA會找不到檔案,如果我想以"OK.xlsx"去找尋檔案,也就是把"- 5月"省略,請問語法要怎麼寫?
Dim mFile As String, 來源檔 As Workbook, 目的檔 As Workbook, Sh As Worksheet, xSh As Worksheet, i As String
mFile = Dir("D:\蘆竹共用\倉儲共用\?日班理貨換算表\108.5月\OK*.XLSX") '開啟指定名稱開頭的檔案
Do While mFile <> ""
Workbooks.Open Filename:="D:\蘆竹共用\倉儲共用\?日班理貨換算表\108.5月\" & mFile
mFile = Dir()
Loop
Set 來源檔 = Workbooks("理貨單_All.xlsx")
Set Sh = 來源檔.Sheets("比菲多.OK")
Set 目的檔 = Workbooks("OK.xlsx")
Set xSh = 目的檔.Sheets(i)
複製代碼
作者:
ikboy
時間:
2019-5-2 15:18
Sub zz()
Dim mFile As String, 來源檔 As Workbook, 目的檔 As Workbook, Sh As Worksheet, xSh As Worksheet, i As String
Dim MyPath
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "D:\蘆竹共用\倉儲共用"
.Title = "日班理貨換算表"
.Show
If .SelectedItems.Count Then MyPath = .SelectedItems(1) Else Exit Sub
End With
mFile = Dir(MyPath & "\" & "OK*.XLSX") '開啟指定名稱開頭的檔案
Do While mFile <> ""
Workbooks.Open Filename:=MyPath & "\" & mFile
mFile = Dir()
Loop
Set 來源檔 = Workbooks("理貨單_All.xlsx")
Set Sh = 來源檔.Sheets("比菲多.OK")
Set 目的檔 = Workbooks("OK.xlsx")
Set xSh = 目的檔.Sheets(i)
End Sub
複製代碼
作者:
PJChen
時間:
2019-5-2 20:37
回復
2#
ikboy
您好,
完整路徑是這樣
"D:\蘆竹共用\倉儲共用\?日班理貨換算表\108.5月\OK*.XLSX"
但某些字出不來,變成?
而 OK*.XLSX我也希望能用模糊字串去代替精確的檔名,以防有人修改檔名時打不開
測試的結果,只能開啟"倉儲共用"的對話框,其他更下一層的子資料夾及檔案都無法開啟!
作者:
PJChen
時間:
2019-5-2 23:19
本帖最後由 PJChen 於 2019-5-2 23:24 編輯
回復
2#
ikboy
謝謝您,
我試了一整晚,終於可以用了,路徑還是無法用*取代,但總算可以打開檔案.
Dim mFile As String, mF As String, 來源檔 As Workbook, 目的檔 As Workbook, Sh As Worksheet, xSh As Worksheet, i As String
Path = "D:\蘆竹共用\倉儲共用\全台理貨換算表\" '來源資料夾"
mFile = Dir(Path & "*萊爾富*.XLSX") '來源檔名
Do While mFile <> ""
Application.DisplayAlerts = False '一般提警示訊息關閉
Workbooks.Open Filename:=Path & mFile
mFile = Dir
Loop
i = Format(Date, "D") + 1 '以日期設定作為工作表的變數
'**********************
Set 來源檔 = Workbooks("理貨單_All.xlsx")
Set Sh = 來源檔.Sheets("比菲多.萊爾富")
mF = Dir(Path & "*五股萊*.XLSX")
mF = Dir
Set 目的檔 = Workbooks(Dir(Path & "*五股萊*.XLSX")) '模糊檔名
Set xSh = 目的檔.Sheets(i)
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)