簡體   English   中英

Excel工作表:將數據從一個工作簿復制到另一個工作簿

[英]Excel sheet: Copy data from one workbook to another workbook

我無法將數據從一個工作簿復制到另一個工作簿。 但是在同一個工作簿中它的工作。 運行宏程序后,目標工作表為空。 我有2個代碼。 兩者都不起作用。 我的源文件是.xlsx格式,目標文件是.xlsm格式。 有什么錯誤嗎?

代碼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代碼2 Excel宏,說copy_paste.xlsm

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

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粘貼類型用作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

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

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

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

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打開它們

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM