- 帖子
- 471
- 主題
- 121
- 精華
- 0
- 積分
- 579
- 點名
- 0
- 作業系統
- WIN10
- 軟體版本
- OFFICE2019
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2015-4-16
- 最後登錄
- 2023-1-17
|
[發問] [已解決]關於 如何選擇多個檔案後依序執行巨集
本帖最後由 starry1314 於 2015-6-25 15:01 編輯
想請問如何讓選取到的檔案依序執行完我要的巨集在關閉檔案後 繼續執行下一個檔案重複到無檔案為止呢?
解決方案- Sub 開啟多個檔案()
- Dim filenames As Variant
- ' set the array to a variable and the True is for multi-select
- filenames = Application.GetOpenFilename(, , , , True)
- counter = 1
- ' 選擇總共要開啟的檔案
- While counter <= UBound(filenames)
- '打開選定的文件,且不更新連結
- Workbooks.Open filenames(counter), UpdateLinks:=0
-
- '巨集
-
- '關閉選擇的檔案
- ActiveWorkbook.Close True
-
-
- ' 消息框 顯示文件名
- 'MsgBox filenames(counter)
- '開啟新檔案
- counter = counter + 1
- Wend
- End Sub
複製代碼 開啟所有選擇的檔案- Sub 開啟多個檔案()
- Dim filenames As Variant
- ' set the array to a variable and the True is for multi-select
- filenames = Application.GetOpenFilename(, , , , True)
- counter = 1
- ' 選擇總共要開啟的檔案
- While counter <= UBound(filenames)
- '打開選定的文件
- Workbooks.Open filenames(counter)
- ' 消息框 顯示文件名
- 'MsgBox filenames(counter)
- '開啟新檔案
- counter = counter + 1
- Wend
- End Sub
複製代碼 此程式為讀取檔案路徑,並在H1依序往下貼上路徑- Sub 開啟檔案_Click()
- With Application.FileDialog(msoFileDialogOpen)
- .InitialFileName = "\\boss-pc\公用磁區"
- .AllowMultiSelect = True
- .Show
- For i = 1 To .SelectedItems.Count
- Cells(i, 7) = .SelectedItems(i) 'i=列 7=欄
- 'MsgBox .SelectedItems(i) '讀取到的資訊
- Next
- End With
- End Sub
複製代碼 |
|