簡體   English   中英

將工作表從一個工作簿復制到另一個工作簿

[英]Copying worksheet from one workbook to another

我是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

代碼運行時沒有編譯錯誤,但也沒有發生任何事情。

試試這個,讓我知道它是否有效

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
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

暫無
暫無

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

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