不同台電腦執行同個xlsm檔案,有些人卻出現下列錯誤,麻煩各位大大解惑,每台電腦都是Microsoft 365
執行階段錯誤 '1004':
Excel無法將工作表插入目的地活頁簿,因為它包含的列和欄比來源活頁簿少。若要移動或複製資料至目的地活頁簿,您可以選取資料,然後使用[複製]與[貼上]命令將它插入另一個活頁簿的工作表。
以下是我的程式碼
Sub 按鈕2_Click()
Dim response As VbMsgBoxResult
response = MsgBox("確定要建立留存檔案?", vbQuestion + vbYesNo, "建立留存檔案")
If response = vbYes Then
Dim wsA1 As Worksheet
Dim wsA2 As Worksheet
Dim wsA3 As Worksheet
Set wsA1 = ActiveWorkbook.Worksheets("報價摘要")
Set wsA2 = ActiveWorkbook.Worksheets("報價明細")
Set wsA3 = ActiveWorkbook.Worksheets("報價封面")
Set SummarySheet = ActiveWorkbook.Worksheets("報價摘要")
' 設定另存新檔的路徑和檔名
Dim folderPath As String
Dim fileName As String
folderPath = "W:\01"
fileName = SummarySheet.Range("G5") & ".pdf"
' 創建一個新的工作簿
Dim newWorkbook As Workbook
Set newWorkbook = Workbooks.Add
' 將報價摘要、報價明細和報價封面工作表複製到新的工作簿
wsA1.Copy After:=newWorkbook.Sheets(newWorkbook.Sheets.Count)
wsA2.Copy After:=newWorkbook.Sheets(newWorkbook.Sheets.Count)
wsA3.Copy After:=newWorkbook.Sheets(newWorkbook.Sheets.Count)
' 刪除新工作簿中的預設工作表
Application.DisplayAlerts = False
newWorkbook.Sheets(1).Delete
Application.DisplayAlerts = True
' 列印為 PDF
newWorkbook.PrintOut ActivePrinter:="Microsoft Print to PDF", PrintToFile:=True, PrToFileName:=folderPath & fileName
' 關閉新工作簿
newWorkbook.Close SaveChanges:=False
' 釋放物件記憶體
Set newWorkbook = Nothing
MsgBox "PDF 輸出完成!"
End If
End Sub