1. 程式人生 > >把一個excel中的sheet拷貝到另外一個excel中

把一個excel中的sheet拷貝到另外一個excel中

     任務描述 :把 source.xls 中的某個sheet 拷貝到 target.xls中去(注意,刪除了source.xls中的sheet)。  目標檔名、原檔名、拷貝的sheet名存在當前xls vba檔案的A2 B2 C2單元格中 。

Sub 按鈕1_Click()
Dim source As Workbook
Dim target As Workbook
Dim name1 As String
Dim name2 As String
Dim sheetname As String
Application.DisplayAlerts = False

tname = ThisWorkbook.Sheets(1).[A2]
sname = ThisWorkbook.Sheets(1).[B2]
sheetname = ThisWorkbook.Sheets(1).[C2]

Set target = Workbooks.Open(ThisWorkbook.Path & "\" & tname)
Set source = Workbooks.Open(ThisWorkbook.Path & "\" & sname)
target.Sheets(sheetname).Delete
source.Sheets(sheetname).Copy after:=target.Sheets(target.Sheets.Count)
        
target.Save
target.Close
source.Close
Application.DisplayAlerts = True
End Sub