【发布时间】:2014-07-03 03:36:54
【问题描述】:
我是 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
代码运行时没有编译错误,但也没有任何反应。
【问题讨论】:
-
你怎么知道代码运行并且没有在两者之间退出?如果确实如此,您能否设置断点并检查您认为问题可能出在哪里?
-
为什么不直接使用 .Copy() 和 .Save 方法
-
@hnk - 我运行它时没有出现编译错误
-
@Sky 我该怎么做???
标签: excel vba worksheet copying