Try   HackMD

求教:Excel不同活頁簿是否能同時連動所有資料及版面?

LHB阿好伯, Jan 26, 2019

tags: Excel

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
問題紀錄

想把某個活頁簿的全部資料
同步在另一個活頁簿裏面
但各式版面要一模一樣
當更改A活頁簿,B活頁簿就會同步更新

初步構想解決方案

在excel上的格式操作都可以紀錄成程式碼
利用 vba 檔案路徑可以用一段程式碼取得想要改變的檔案路徑
在利用簡單迴圈將上面錄製的各式操作進行迴圈處理

程式碼

Sub 選取檔案() Dim fDialog As FileDialog Set fDialog = Application.FileDialog(msoFileDialogFilePicker) ' 允許選擇多檔案 fDialog.AllowMultiSelect = True ' 視窗標題 fDialog.Title = "請選擇 Excel 檔案" ' 初始目錄 fDialog.InitialFileName = "C:\" ' 設定檔案類型 fDialog.Filters.Clear fDialog.Filters.Add "Excel 檔案", "*.xlsx" fDialog.Filters.Add "所有檔案", "*.*" If fDialog.Show = -1 Then ' 逐一處理每個檔案 For Each file In fDialog.SelectedItems Cells.Select '儲存格全選 Selection.Copy '複製格式 Workbooks.Open Filename:=file '開啟檔案 Cells.Select Selection.PasteSpecial '貼上格式 Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Application.CutCopyMode = False Range("A1").Select ActiveWorkbook.Save ActiveWindow.Close Range("A1").Select Next file End If End Sub

使用步驟

開啟開發人員選項

首先到檔案中最下面的選項

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

選擇自訂功能區 -> 開發人員

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

選擇開發人員中的VBA編輯器

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

新增一個模組

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

貼上程式碼執行
選擇要變更的檔案

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

後續若是還要變更可以點選巨集中的執行

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

或是新增一個快捷鍵使用

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後存檔時請選擇XXX.xlsm檔案格式

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

檔案下載

參考資料


Excel VBA 職場即用255招:不會寫程式也能看懂的VBA無痛指導

https://officeguide.cc/excel-vba-file-folder-selection-dialog-tutorial/

全文分享至

https://www.facebook.com/LHB0222/

有疑問想討論的都歡迎於下方留言

喜歡的幫我分享給所有的朋友 \o/

有所錯誤歡迎指教

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →