将工作表从一个工作簿复制到另一个工作簿

时间:2014-07-03 03:36:55

标签: excel vba worksheet copying

我是Excel新手,我正试图让我的VBA使用此代码将工作表复制到另一个工作簿:

Sub CopySheetl()
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shtToCopy As Worksheet

Set wkbSource = Workbooks.Open("C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\June Lines Report.xlsm")
Set wkbDest = Workbooks.Open("C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\2014 Lines Report.xlsx")

'set shttocopy = wkbsoure.sheets("name of worksheet")

Set shtToCopy = wkbSource.Sheets(9)

shtToCopy.PasteSpecial wkbDest.Sheets(1)

wkbDest.Save


End Sub

最终结果一无所获。什么都没发生。我的意思是代码运行良好但它什么也没做。我需要帮助!

编辑:

我按照建议尝试了另一个代码:

Sub copy_sh()

Dim sourceSheet As Worksheet
Dim destSheet As Worksheet

'' copy from the source
Workbooks.Open filename:="C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\June Lines Report.xlsm"
Set sourceSheet = Worksheets("JuneSummary")
sourceSheet.Activate
sourceSheet.Cells.Select
Selection.copy

'' paste to the destination
Workbooks.Open filename:="C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\2014 Lines Report.xlsx"
Set destSheet = Worksheets("June")
destSheet.Activate
destSheet.Cells.Select
destSheet.Paste

'' save & close
ActiveWorkbook.Save
ActiveWorkbook.Close

End Sub

代码运行时没有编译错误,但也没有发生任何事情。

2 个答案:

答案 0 :(得分:0)

试试这个,让我知道它是否有效

Sub CopySheetl()
    Dim wkbSource As Workbook
    Dim wkbDest As Workbook
    Dim shtToCopy As Worksheet

    Set wkbSource = Workbooks.Open("C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\June Lines Report.xlsm")
    Set wkbDest = Workbooks.Open("C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\2014 Lines Report.xlsx")

    'set shttocopy = wkbsoure.sheets("name of worksheet")

    ‘Set shtToCopy = wkbSource.Sheets(9)

    ‘shtToCopy.PasteSpecial wkbDest.Sheets(1)

    wkbSource.Sheets(9).Copy after:=wkbDest.Sheets(1)

    wkbDest.Save


End Sub

答案 1 :(得分:0)

Sub CopySheet2()
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shtToCopy As Worksheet

Set wkbSource = Workbooks.Open("C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\June Lines Report.xlsm")
Set wkbDest = Workbooks.Open("C:\Documents and Settings\wong.zuowei\My Documents\Dropbox\SIP\Report Templates\New Documentation System\2014\2014 Lines Report.xlsx")

'set shttocopy = wkbsoure.sheets("name of worksheet")

Set shtToCopy = wkbSource.Sheets(9)

shtToCopy.Copy After:=wkbDest.Sheets(1)

wkbDest.Save


End Sub