2016-06-21 195 views
3

我無法將數據從一個工作簿複製到另一個工作簿。但在同一個工作簿中工作。運行宏程序後,目標工作表爲空。我有2個代碼。兩者都不起作用。我的源文件是.xlsx格式和目標文件是.xlsm格式。有什麼錯誤嗎?Excel工作表:將數據從一個工作簿複製到另一個工作簿

代碼1:

Sub mycode() 

Workbooks.Open Filename:="source_file" 
Worksheets("Sheet1").Cells.Select 
Selection.Copy 


Workbooks.Open Filename:="destination_file" 
Worksheets("Sheet1").Cells.Select 
Selection.PasteSpecial 
ActiveWorkbook.Save 


End Sub 

代碼2

Sub foo2() 
Dim x As Workbook 
Dim y As Workbook 

Set x = Workbooks.Open("source file") 
Set y = Workbooks.Open("destination file") 

y.Sheets("Sheet1").Range("A1").Value = x.Sheets("Sheet1").Range("A1") 

x.Close 

End Sub 

回答

1

我假設你正在寫下面代碼1代碼2 Excel宏在單獨的文件,說copy_paste.xlsm

代碼1工作時,您提供給Workbooks.open文件的完整路徑:

Sub mycode() 

Workbooks.Open Filename:="C:\Users\xyz\Documents\Excel-Problem\source_file.xlsx" 
Worksheets("Sheet1").Cells.Select 
Selection.Copy 

Workbooks.Open Filename:="C:\Users\xyz\Documents\Excel-Problem\destination_file.xlsm" 
Worksheets("Sheet1").Cells.Select 
Selection.PasteSpecial xlPasteValues    'xlPasteAll to paste everything 
ActiveWorkbook.Save 

ActiveWorkbook.Close SaveChanges:=True    'to close the file 
Workbooks("source_file").Close SaveChanges:=False 'to close the file 

End Sub 

要粘貼的一切(公式+值+格式),用漿糊類型xlPasteAll

代碼2是工作壓力太大,你需要的是提供完整路徑和您在文件名失蹤_

Sub foo2() 
Dim x As Workbook 
Dim y As Workbook 

Set x = Workbooks.Open("C:\Users\xyz\Documents\Excel-Problem\source_file.xlsx") 
Set y = Workbooks.Open("C:\Users\xyz\Documents\Excel-Problem\destination_file.xlsm") 

'it copies only Range("A1") i.e. single cell 
y.Sheets("Sheet1").Range("A1").Value = x.Sheets("Sheet1").Range("A1") 

x.Close SaveChanges:=False 
y.Close SaveChanges:=True 

End Sub 
+0

謝謝你甚至打開都沒有。我只提供了完整的路徑,供參考我提到的源文件。我試過你的代碼,它不工作。在打開第二個文件後調試時,調試器沒有進入下一行,而是停止調試本身 – Star

+0

Whar是錯誤信息? –

+0

第二個文件是一個啓用宏的文件,那麼當你手動打開它時,這個文件中是否有宏? –

1

編輯添加(最小)文件檢查

你必須指定完整的文件路徑,名稱和擴展名

你可以只打開目標文件,像這樣

Option Explicit 

Sub foo2() 
    Dim y As Workbook 
    Dim sourcePath As String, sourceFile As String, destFullPath As String '<--| not necessary, but useful not to clutter statements 

    sourcePath = "C:\Users\xyz\Documents\Excel-Problem\" '<--| specify your source file path down to the last backslash and with no source file name 
    sourceFile = "source_file.xlsx" '<--| specify your source file name only, with its extension 
    destFullPath = "C:\Users\xyz\Documents\Excel-Problem\destination_file.xlsm" '<--| specify your destination file FULL path 

    If Dir(destFullPath) = "" Then '<--| check is such a file actually exists 
     MsgBox "File " & vbCrLf & vbCrLf & destFullPath & vbCrLf & vbCrLf & "is not there!" & vbCrLf & vbCrLf & vbCrLf & "The macro stops!", vbCritical 
    Else 
     Set y = Workbooks.Open(destFullPath) 

     With y.Sheets("Sheet1").Range("A1") 
      .Formula = "='" & sourcePath & "[" & sourceFile & "]Sheet1'!$A$1" 
      .Value = .Value 
     End With 

     y.Close SaveChanges:=True 
    End If 
End Sub 

,你可以使用Excel4macro

+0

編輯添加文件檢查。你有任何'在錯誤恢復下一個'或'在錯誤轉到'語句? – user3598756

相關問題