如何使用 vba 将工作表复制到另一个工作簿? [英] How to copy sheets to another workbook using vba?
问题描述
所以,一般来说,我想做的是复制一份工作簿.但是,源工作簿正在运行我的宏,我希望它制作自己的相同副本,但没有宏.我觉得应该有一种简单的方法可以用 VBA 做到这一点,但还没有找到.我正在考虑将工作表一张一张地复制到我将创建的新工作簿中.我该怎么做?有没有更好的办法?
So, what I want to do, generally, is make a copy of a workbook. However, the source workbook is running my macros, and I want it to make an identical copy of itself, but without the macros. I feel like there should be a simple way to do this with VBA, but have yet to find it. I am considering copying the sheets one by one to the new workbook, which I will create. How would I do this? Is there a better way?
推荐答案
Ozgrid 回答了类似的问题.基本上,您只需将每张纸从 Workbook1 一次复制到 Workbook2.
Someone over at Ozgrid answered a similar question. Basically, you just copy each sheet one at a time from Workbook1 to Workbook2.
Sub CopyWorkbook()
Dim currentSheet as Worksheet
Dim sheetIndex as Integer
sheetIndex = 1
For Each currentSheet in Worksheets
Windows("SOURCE WORKBOOK").Activate
currentSheet.Select
currentSheet.Copy Before:=Workbooks("TARGET WORKBOOK").Sheets(sheetIndex)
sheetIndex = sheetIndex + 1
Next currentSheet
End Sub
免责声明:我没有尝试过这段代码,而是采用了链接示例来解决您的问题.如果不出意外,它应该会引导您找到预期的解决方案.
Disclaimer: I haven't tried this code out and instead just adopted the linked example to your problem. If nothing else, it should lead you towards your intended solution.
这篇关于如何使用 vba 将工作表复制到另一个工作簿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!